Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does list comprehension not filter out duplicates?

I have a workaround to the following question. That workaround would be a for loop with a test for inclusion in the output like the following:

#!/usr/bin/env python

def rem_dup(dup_list):
    reduced_list = []
    for val in dup_list:
        if val in reduced_list:
            continue
        else:
            reduced_list.append(val)

    return reduced_list

I am asking the following question, because I am curious to see if there is a list comprehension solution.

Given the following data:

reduced_vals = []
vals = [1, 2, 3, 3, 2, 2, 4, 5, 5, 0, 0]

Why does

reduced_vals = = [x for x in vals if x not in reduced_vals]

produce the same list?

>>> reduced_vals
[1, 2, 3, 3, 2, 2, 4, 5, 5, 0, 0]

I think it has something to do with checking the output (reduced_vals) as part of an assignment to a list. I am curious, though as to the exact reason.

Thank you.

like image 980
octopusgrabbus Avatar asked Jul 16 '12 13:07

octopusgrabbus


People also ask

Is list comprehension faster than filter?

Actually, list comprehension is much clearer and faster than the filter+lambda combination, but you can use whichever you find easier.

How do I avoid duplicates in my list?

If you don't want duplicates, use a Set instead of a List . To convert a List to a Set you can use the following code: // list is some List of Strings Set<String> s = new HashSet<String>(list); If really necessary you can use the same construction to convert a Set back into a List .

What are the advantages of using list comprehensions?

One main benefit of using a list comprehension in Python is that it's a single tool that you can use in many different situations. In addition to standard list creation, list comprehensions can also be used for mapping and filtering. You don't have to use a different approach for each scenario.


1 Answers

The list comprehension creates a new list, while reduced_vals points to the empty list all the time during the evaluation of the list comprehension.

The semantics of assignments in Python are: Evaluate the right-hand side and bind the resulting object to the name on the left-hand side. An assignment to a bare name never mutates any object.

By the way, you should use set() or collections.OrderedDict.fromkeys() to remove duplicates in an efficient way (depending on whether you need to preserve order or not).

like image 168
Sven Marnach Avatar answered Sep 28 '22 09:09

Sven Marnach