efficient list merging

François Pinard pinard at iro.umontreal.ca
Tue Sep 3 15:19:44 EDT 2002

[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()

It seems to work:

>>> list1 = list('abcdebxyz')
>>> list2 = list('pqqr')
>>> list1
['a', 'b', 'c', 'd', 'e', 'b', 'x', 'y', 'z']
>>> list2
['p', 'q', 'q', 'r']
>>> dict(zip(list1 + list2, [None] * (len(list1) + len(list2)))).keys()
['a', 'c', 'b', 'e', 'd', 'q', 'p', 'r', 'y', 'x', 'z']

François Pinard   http://www.iro.umontreal.ca/~pinard

More information about the Python-list mailing list