unique elements from list of lists

Peter Otten __peter__ at web.de
Fri Feb 9 06:11:19 EST 2007


Tekkaman wrote:

> I have a list of lists and I want to define an iterator (let's call
> that uniter) over all unique elements, in any order. For example,
> calling:
> 
> sorted(uniter([['a', 'b', 'd'], ['b', 'c'], ['a', 'c', 'd']]))
> 
> must return ['a', 'b', 'c', 'd']. I tried the following
> implementations:
> 
> from itertools import chain
> def uniter1(listOfLists):
>     for item in set(chain(*listOfLists)): yield item

def uniter(lists):
   return iter(set(chain(*lists)))

This avoids the explicit for-loop. 

Peter





More information about the Python-list mailing list