How to remove duplicate items from a list using list comprehension? I have following code:
a = [1, 2, 3, 3, 5, 9, 6, 2, 8, 5, 2, 3, 5, 7, 3, 5, 8]
b = []
b = [item for item in a if item not in b]
but it doesn't work, just produces identical list. Why its producing an identical list?
The reason that the list is unchanged is that
b
starts out empty. This means thatif item not in b
is alwaysTrue
. Only after the list has been generated is this new non-empty list assigned to the variableb
.If you don't mind using a different technique than list comprehension you can use a set for that:
Use
keys
on adict
constructed with values ina
as its keys.Or use a set:
It's producing an identical list as
b
contains no elements at run-time. What you'd want it this:Use groupby:
Leave out the key argument if you don't care about which order the value first appeared in the original list, e.g.
You can do some cool things with
groupby
. To identify items that appear multiple times:Or to build up a frequency dictionary:
There are some very useful functions in the itertools module:
chain
,tee
andproduct
to name a few!