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.
The list comprehension creates a new list, while
reduced_valspoints 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()orcollections.OrderedDict.fromkeys()to remove duplicates in an efficient way (depending on whether you need to preserve order or not).