[Tutor] setting lists

Kent Johnson kent37 at tds.net
Wed Jan 10 14:04:56 CET 2007


Switanek, Nick wrote:
> I'd like to remove redundant items from a list, and have read that using
> set() is an effective way to do it. But I often get the following error,
> and I'd be glad for your help understanding what's wrong.
> 
>>>> type(n)		# This is a list of lists of strings
> <type 'list'>
>>>> n1 = list(set(n))
> Traceback (most recent call last):
>   File "<interactive input>", line 1, in ?
> TypeError: list objects are unhashable

So you have a list of lists of strings and you want to remove 
duplicates? The problem is that sets (and dicts) require that their 
values (or keys) be hashable, which means that a unique, unchanging 
reference number can be computed from the value. Practically speaking 
this means that the value must be immutable (unable to be changed) which 
excludes lists.

The solution is to turn the list into something immutable. One way would 
be to convert the lists to tuples:
n1 = list(set(tuple(i) for i in n))

This gives you a list of tuples, if you need a list of lists you will 
have to convert back which you can do with a list comprehension:
n1 = [ list(j) for j in set(tuple(i) for i in n)) ]

Another way would be to use the string representation of the list as a 
dictionary key and the original list as the value, then pull the 
original lists back out:
n1 = dict((repr(i), i) for i in n).values()

Kent



More information about the Tutor mailing list