unique elements from list of lists

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Fri Feb 9 08:52:55 EST 2007


Tekkaman:

If the sublists contain hashable elements you can use this:

def uniter(lists):
    merge = set()
    for sub in lists:
        merge = merge.union(sub)
    for el in merge:
        yield el

data = [['a', 'b', 'd'], ['b', 'c'], ['a', 'c', 'd']]
print list(uniter(data))

But often this too may be enough:

def uniter(lists):
    merge = set()
    for sub in lists:
        merge = merge.union(sub)
    return merge

Bye to the gentle Pegas too,
bearophile




More information about the Python-list mailing list