efficient list merging

Peter Saffrey theoryboy at my-deja.com
Tue Sep 3 14:12:38 EDT 2002


I have two lists of lists of strings which I would like to merge
efficiently without repeats. I assume that each list currently
contains no repeats. At the moment, I have two possible
implementations:

for item in list2:
   if item not in list1:
      list1.append(item)

which is obvious, and I suspect slow and

list1hash = {}
for item in list1:
   itemtxt = '%s' % item
   list1hash[itemtxt] = 1

for item in list2:
   itemtxt = '%s' % item
   if not list1hash.has_key(itemtxt)
   list1hash.append(item)

which I thought would be quicker because it uses a hash, but it
doesn't seem to be from my crude tests. Maybe it's the text conversion
which is slowing it down - the strings stored in the inner lists are
quite long (about 60 characters, and each inner list has up to 8
items) is there a way I can index the hash by the address of the
lists? I converted to a string just as a "this is the first thing I
can think of" approach.

Or is there a better way?

Thanks,

Peter



More information about the Python-list mailing list