[Tutor] List processing question - consolidating duplicate entries

Kent Johnson kent37 at tds.net
Wed Nov 28 13:52:02 CET 2007


Michael Langford wrote:
> What you want is a set of entries.

Not really; he wants to aggregate entries.

> # remove duplicate entries
> #
> #  myEntries is a list of lists,
> #    such as [[1,2,3],[1,2,"foo"],[1,2,3]]
> #
> s=set()
> [s.add(tuple(x)) for x in myEntries]

A set can be constructed directly from a sequence so this can be written as
  s=set(tuple(x) for x in myEntries)

BTW I personally think it is bad style to use a list comprehension just 
for the side effect of iteration, IMO it is clearer to write out the 
loop when you want a loop.

Kent


More information about the Tutor mailing list