efficient list merging
François Pinard
pinard at iro.umontreal.ca
Tue Sep 3 20:48:35 EDT 2002
[Christophe Delord]
>> I am not sure about the relative speed of everything, but I would be
>> tempted to try:
>>
>> dict(zip(list1 + list2, [None] * (len(list1) + len(list2)))).keys()
>
> This may be written using list comprehension:
>
>>>> dict([(k,1) for k in list1+list2]).keys()
The `for' loop, even within a list comprehension, will be compiled into an
explicit loop, likely to be slower than the implicit loops within `zip'.
Taking advantage of other clever suggestions, I would guess that:
both = list1 + list2
result = dict(zip(both, both)).keys()
would not be so bad, speed-wise.
--
François Pinard http://www.iro.umontreal.ca/~pinard
More information about the Python-list
mailing list