I need to add items to the list only if the current iterated item is not already listed.
>>> l = [1, 2]
>>> for x in (2, 3, 4):
... if x not in l:
... l.append(x)
...
>>> l
[1, 2, 3, 4]
against
>>> l = [1, 2]
>>> [l.append(i) for i in (2, 3, 4) if i not in l]
[None, None]
>>> l
[1, 2, 3, 4]
Understanding the list gives the result - this is what I want, just the returned list is useless. Is this a good example of using lists?
Iteration is a good solution, but I wonder if there is a more idiomatic way to do this?
source
share