[Tutor] setting lists
Kent Johnson
kent37 at tds.net
Wed Jan 10 21:06:49 CET 2007
Kent Johnson wrote:
> 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()
You could combine these two, use a tuple as the key but retain the list:
n1 = dict((tuple(i), i) for i in n).values()
I think I like this the best, it avoids the conversion from tuple back
to list, and I suspect that converting a list to a tuple - which should
involve just copying references to the list contents - is quicker than
converting it to a string.
Kent
More information about the Tutor
mailing list