efficient list merging

Christophe Delord christophe.delord at free.fr
Tue Sep 3 16:09:44 EDT 2002


On Tue, 03 Sep 2002 15:19:44 -0400
pinard at iro.umontreal.ca (François Pinard) wrote:

> [Peter Saffrey]
> 
> > I have two lists of lists of strings which I would like to merge
> > efficiently without repeats.  [...] Or is there a better way?
> 
> 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:

>>> list1 = list('abcdebxyz')
>>> list2 = list('pqqr')
>>> dict([(k,1) for k in list1+list2]).keys()
['a', 'c', 'b', 'e', 'd', 'q', 'p', 'r', 'y', 'x', 'z']





-- 

(o_   Christophe Delord                   _o)
//\   http://christophe.delord.free.fr/   /\\
V_/_  mailto:christophe.delord at free.fr   _\_V



More information about the Python-list mailing list