[Tutor] List processing question - consolidating duplicate entries

Michael Langford mlangford.cs03 at gtalumni.org
Wed Nov 28 06:10:19 CET 2007

What you want is a set of entries. Unfortunately, python lists are not
"hashable" which means you have to convert them to something hashable
before you can use the python set datatype.

What you'd like to do is add each to a set while converting them to a
tuple, then convert them back out of the set. In python that is:

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

List completions are useful for all sorts of list work, this included.

Do not use a database, that would be very ugly and time consuming too.

This is cleaner than the dict keys approach, as you'd *also* have to
convert to tuples for that.

If you need this in non-list completion form, I'd be happy to write
one if that's clearer to you on what's happening.

Michael Langford
Phone: 404-386-0495
Consulting: http://www.RowdyLabs.com

More information about the Tutor mailing list