I have to append elements to a list only if the current iterated element is not already in the list.
>>> l = [1, 2]
>>> for x in (2, 3, 4):
... if x not in l:
... l.append(x)
...
>>> l
[1, 2, 3, 4]
vs
>>> l = [1, 2]
>>> [l.append(i) for i in (2, 3, 4) if i not in l]
[None, None]
>>> l
[1, 2, 3, 4]
The list comprehension gives the result is what I want, just the returned list is useless. Is this a good use case for list comprehensions?
The iteration is a good solution, but I'm wondering if there is a more idiomatic way to do this?
This algorithm, either with or without a list comprehension, is not as efficient as possible; list.__contains__
is O(n), and so adding the elements of another list to it is O(n2). On the other hand, set.__contains__
is O(log n), so the best way to do this is to use a set to check for membership, and a list to preserve order. That way you're doing n operations that are O(log n), for a total of O(n log n), which much faster than O(n2) for reasonable values of n (above say, 100 elements).
>>> l = [1, 2]
>>> seen = set(l)
>>> for x in (2, 3, 4):
... if x not in seen:
... seen.add(x)
... l.append(x)
...
>>> l
[1, 2, 3, 4]
>>>
You could do:
l.extend((i for i in (2,3,4) if i not in l))
This solution still works if the added list is non-unique.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With