[Tutor] using lists as values for key in dictionary?

Marc Tompkins marc.tompkins at gmail.com
Wed Oct 15 08:54:46 CEST 2008


Dictionary keys must be immutable - in other words, they must be objects
that aren't allowed to change.  Lists can change, so the answer is no.
However, tuples are immutable, so if you turn your list into a tuple (I can
never remember, is that a cast or a coercion?), you can use it as a
dictionary key.

>>> lX = [1, 2, 3, 'guido']
>>> lX
[1, 2, 3, 'guido']
>>> lX = tuple(lX)
>>> lX
(1, 2, 3, 'guido')

-- 
www.fsrtechnologies.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20081014/d0c8fc97/attachment.htm>


More information about the Tutor mailing list