Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is list comprehension appropriate here?

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?

like image 480
Paolo Avatar asked Aug 04 '11 01:08

Paolo


2 Answers

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]
>>> 
like image 94
SingleNegationElimination Avatar answered Nov 14 '22 14:11

SingleNegationElimination


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.

like image 41
Gerrat Avatar answered Nov 14 '22 13:11

Gerrat