0

I need to make a dictionary where you can reference [[1,2],[3,4]] --> ([1,2]:0, [2,3]:0) I've tried different ways but I can't use a list in a dictionary. So i tried using tuples, but its still the same. Any help is appreciated!

asked Nov 15, 2012 at 22:19
3
  • If you are trying to use a list as a key I think you may have the wrong implementation for your algorithm. Commented Nov 15, 2012 at 22:22
  • 1
    So i tried using tuples, but its still the same Please show us your code! Commented Nov 15, 2012 at 22:23
  • Why do you want to do this? If the content of the lists isn't going to change, and you want to use the content as the key in your dict, then convert the list to tuple and use that as a key, as others have suggested. On the other hand, if you're going to be changing the content of the list but still want to be able to map the list to the same value in the dictionary, then call the id() builtin function on your list and use the result of that as your key. These are two very different use cases and you haven't given enough information for us to determine which approach solves your problem. Commented Nov 15, 2012 at 22:30

3 Answers 3

4

You need to use tuples:

dict.fromkeys((tuple(i) for i in [[1,2],[3,4]]), 0)

or (for python2.7+)

{tuple(i): 0 for i in [[1,2], [3,4]]}

Edit:

Reading the comments, OP probably want to count occurrences of a list:

>>> collections.Counter(tuple(i) for i in [[1,2], [1,2], [3,4]])
Counter({(1, 2): 2, (3, 4): 1})
answered Nov 15, 2012 at 22:21
Sign up to request clarification or add additional context in comments.

19 Comments

Is there any way to change the 0? Like, if you have a list of [[1,2],[1,2],[2,3]], I need it to return [[1:2]:2, [2:3]:1]?
@user1828072 Of course, just write 1 as the last argument instead of 0
@user1828072 Oh, if you need to count the number of times a list is inside another, you'll probably need collections.Counter
In the first version, I prefer dict((tuple(i),0) for i in [[1,2],[3,4]]) rather than using the classmethod -- But that's just a matter of taste I suppose. (It's also closer to the syntax used to create the dict-comp in py2.7)
As you can probably tell, im quite new at this! I mean, the last number '0', instead of it being 0, can it return the number of occurances? For example: y = [[1,2],[1,2],[2,3]] ; there are two [1:2], is there a way to use a variable to return something like:[1,2]:2 instead? Thanks a lot!
|
3

Lists can't be used as dictionary keys since they aren't hashable (probably because they can be mutated so coming up with a reasonable hash function is impossible). tuple however poses no problem:

d = {(1,2):0, (3,4):0}

Note that in your example, you seem to imply that you're trying to build a dictionary like this:

((1,2):0, (3,4):0)

That won't work. You need curly brackets to make a dictionary.

answered Nov 15, 2012 at 22:22

Comments

0

([1,2]:0, [2,3]:0) is not a dictionary. I think you meant to use: {(1,2):0, (2,3):1}

answered Nov 15, 2012 at 22:24

2 Comments

It looks like we had the same idea at the same time -- however, your second example won't work either since you're using lists as keys which won't fly.
Right, it looks like we also noticed that at the same time too! I edited it to reflect. Thanks!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.