Why does removing duplicates from a list produce [None, None] output?

前端 未结 3 616
长情又很酷
长情又很酷 2021-01-28 09:37

I am new to Python and I\'m not able to understand why I am getting the results with None values.

#Remove duplicate items from a list
def remove         


        
3条回答
  •  孤城傲影
    2021-01-28 10:03

    As the other answers have explained, the reason you're getting a list of None values is because list.append returns None, and you're calling it in a list comprehension. That means you're building a list full of None values along side your list of unique values.

    I would like to suggest that you ditch the list comprehension. Because you need to access outside state (the list of unique values seen so far), a comprehension can't easily do what you want. A regular for loop is much more appropriate:

    def remove_duplicates(lst):
        unique_list = []
        for item in lst:
            if item not in unique_list:
                unique_list.append(item)
        return unique_list
    

    A more Pythonic approach however would be to use a set to handle the unique items, and to make your function a generator:

    def remove_duplicates(lst):
        uniques = set()
        for item in lst:
            if item not in unique_list:
                yield item
                uniques.add(item)
    

    The itertools.ifilterfase function from the standard library can help improve this even further, as shown in the recipe in the docs (you'll have to scroll down a little to find the specific recipe):

    def unique_everseen(iterable, key=None):
        "List unique elements, preserving order. Remember all elements ever seen."
        # unique_everseen('AAAABBBCCDAABBB') --> A B C D
        # unique_everseen('ABBCcAD', str.lower) --> A B C D
        seen = set()
        seen_add = seen.add
        if key is None:
            for element in filterfalse(seen.__contains__, iterable):
                seen_add(element)
                yield element
        else:
            for element in iterable:
                k = key(element)
                if k not in seen:
                    seen_add(k)
                    yield element
    

提交回复
热议问题