efficient list merging

Ken Seehof kseehof at neuralintegrator.com
Tue Sep 3 16:27:53 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

This is a bit faster, but assumes that the first list does not initially
contain duplicates.  It eliminates duplicates that would be introduced by
merging items from list2.  Also, this maintains the original list order
(i.e. list1+list2 without adding duplicates).

def merge(list1,list2):
    d1 = dict(zip(list1,list1))
    return list1+[s for s in list2 if s not in d1]

- Ken Seehof





More information about the Python-list mailing list