471,054 Members | 1,656 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,054 software developers and data experts.

keys in dictionary

I run the following code and got wrong message, but I still want to
make [1,2],[4,3] and [6,9]
to be keys of the dictionary or change the style a little bit. How to do that?
Thanks!
p=[[1,2],[4,3],[6,9]]
n=dict([(x,[]) for x in p]) Traceback (most recent call last):
File "<interactive input>", line 1, in ?
TypeError: list objects are unhashable

Nov 22 '05 #1
2 1168
Shi Mu wrote:
I run the following code and got wrong message, but I still want to
make [1,2],[4,3] and [6,9] to be keys of the dictionary or change the
style a little bit. How to do that?


Make them tuples:
p=[[1,2],[4,3],[6,9]]
n=dict([(tuple(x),[]) for x in p])
n

{(6, 9): [], (1, 2): [], (4, 3): []}

Regards,
Martin

P.S. Please don't cross-post.
Nov 22 '05 #2
use (1,2) , (3,4)....
"Shi Mu" <sa************@gmail.com> a écrit dans le message de news:
ma**************************************@python.or g...
I run the following code and got wrong message, but I still want to
make [1,2],[4,3] and [6,9]
to be keys of the dictionary or change the style a little bit. How to do
that?
Thanks!
p=[[1,2],[4,3],[6,9]]
n=dict([(x,[]) for x in p]) Traceback (most recent call last):
File "<interactive input>", line 1, in ?
TypeError: list objects are unhashable

Nov 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

57 posts views Thread by Egor Bolonev | last post: by
90 posts views Thread by Christoph Zwerschke | last post: by
13 posts views Thread by Nader | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.