efficient list merging

Peter Saffrey theoryboy at my-deja.com
Wed Sep 4 10:01:48 EDT 2002

I was trying to abstract a bit. The way I get the lists of lists of
strings is far too long and ugly to contemplate posting here. Sorry if
I didn't post compilable code - again I was trying to abstract.

Here's an example:

lol1 = [['a'], ['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i', 'j']]
lol2 = [['d', 'e', 'f'], ['k', 'l', 'm'], ['d'], ['f', 'g'], ['a']]

I want a function which returns

mergelol = [['a'], ['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i',
'j'], ['k', 'l', 'm'], ['d']]

That is the set-union of the two lists of lists. Note that I've used
very short strings and short list lengths here for simplicity. The
strings would be up to 80 characters in length, the sub-lists up to 8
items in length, and the list of lists itself could contain 50


More information about the Python-list mailing list