Wednesday, February 21, 2024
HomeSoftware EngineeringEasy methods to Take away Duplicates from Checklist in Python

Easy methods to Take away Duplicates from Checklist in Python


The problem

Outline a operate that removes duplicates from an array of numbers and returns it because of this.

The order of the sequence has to remain the identical.

The answer in Python code

Choice 1:

def distinct(seq):
    return listing(dict.fromkeys(seq))

Choice 2:

distinct = lambda s: [e for i,e in enumerate(s) if e not in s[:i]]

Choice 3:

def distinct(seq):
    outcome = []
    seen = set()
    for a in seq:
        if a not in seen:
            outcome.append(a)
            seen.add(a)
    return outcome

Take a look at instances to validate our answer

import take a look at
from answer import distinct

@take a look at.describe("Fastened Assessments")
def fixed_tests():
    @take a look at.it('Primary Take a look at Instances')
    def basic_test_cases():
        take a look at.assert_equals(distinct([1]), [1])
        take a look at.assert_equals(distinct([1, 2]), [1, 2])
        take a look at.assert_equals(distinct([1, 1, 2]), [1, 2])
        take a look at.assert_equals(distinct([1, 1, 1, 2, 3, 4, 5]), [1, 2, 3, 4, 5])
        take a look at.assert_equals(distinct([1, 2, 2, 3, 3, 4, 4, 5, 6, 7, 7, 7]), [1, 2, 3, 4, 5, 6, 7])
        
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments