By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,885 Members | 1,499 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,885 IT Pros & Developers. It's quick & easy.

Getting values from keys and Viceversa

100+
P: 440
Hi,

Dictionary works in getting values from the key values.But I would like to get keys from values.

For example :

d = {100: [1,0,0],102: [0,1,0],202: [0,1,1]}

d.get(102)

o/p: [0,1,0]

I need

i/p

[0,1,0]

o/p : 102

Thanks

PSB
Mar 17 '07 #1
Share this Question
Share on Google+
10 Replies


bartonc
Expert 5K+
P: 6,596
Hi,

Dictionary works in getting values from the key values.But I would like to get keys from values.

For example :

d = {100: [1,0,0],102: [0,1,0],202: [0,1,1]}

d.get(102)

o/p: [0,1,0]

I need

i/p

[0,1,0]

o/p : 102

Thanks

PSB
Keeping an inverse dictionary is not uncommon:
Expand|Select|Wrap|Line Numbers
  1.  
  2. >>> d = {'a':1, 'b':2, 'c':3}
  3. >>> di = dict((v, k) for k, v in d.items())
  4. >>> di
  5. {1: 'a', 2: 'b', 3: 'c'}
  6. >>> 
Mar 17 '07 #2

100+
P: 440
Thanks for the reply.

How to handle the dictionary when we have a Key as a number and Values are list?

As I mentioned in my example

-PSB
Mar 18 '07 #3

100+
P: 440
Expand|Select|Wrap|Line Numbers
  1. Sample>>> d = {100: [1,0,0],102: [0,1,0],202: [0,1,1]}
  2. >>> di = dict((v, k) for k, v in d.items())
  3.  
  4. Traceback (most recent call last):
  5.   File "<pyshell#9>", line 1, in -toplevel-
  6.     di = dict((v, k) for k, v in d.items())
  7. TypeError: list objects are unhashable
  8. >>> 
I am getting the above error when I applied for the example I mentioned

-PSB
Mar 18 '07 #4

bvdet
Expert Mod 2.5K+
P: 2,851
Expand|Select|Wrap|Line Numbers
  1. Sample>>> d = {100: [1,0,0],102: [0,1,0],202: [0,1,1]}
  2. >>> di = dict((v, k) for k, v in d.items())
  3.  
  4. Traceback (most recent call last):
  5.   File "<pyshell#9>", line 1, in -toplevel-
  6.     di = dict((v, k) for k, v in d.items())
  7. TypeError: list objects are unhashable
  8. >>> 
I am getting the above error when I applied for the example I mentioned

-PSB
Try this:
Expand|Select|Wrap|Line Numbers
  1. >>> dict(zip([str(x) for x in d.values()], d.keys()))
  2. {'[0, 1, 1]': 202, '[1, 0, 0]': 100, '[0, 1, 0]': 102}
  3. >>> 
Lists are not hashable but strings are.
Mar 18 '07 #5

Expert 100+
P: 511
Hi,

Dictionary works in getting values from the key values.But I would like to get keys from values.

For example :

d = {100: [1,0,0],102: [0,1,0],202: [0,1,1]}

d.get(102)

o/p: [0,1,0]

I need

i/p

[0,1,0]

o/p : 102

Thanks

PSB
usually, the way to get values is through the values() method.
for this case, you can use the dictionary values() method.
eg
Expand|Select|Wrap|Line Numbers
  1. if  [0,1,0] in d.values():
  2.     print "found "
  3.  
Mar 18 '07 #6

100+
P: 440
Try this:
Expand|Select|Wrap|Line Numbers
  1. >>> dict(zip([str(x) for x in d.values()], d.keys()))
  2. {'[0, 1, 1]': 202, '[1, 0, 0]': 100, '[0, 1, 0]': 102}
  3. >>> 
Lists are not hashable but strings are.
So we have to create the list as string and use it for inversing and use it.

Thanks in advance
PSB
Mar 18 '07 #7

bartonc
Expert 5K+
P: 6,596
So we have to create the list as string and use it for inversing and use it.

Thanks in advance
PSB
Not if you use tuples instead of lists.
Lists let you assign into them (they are mutable) as in
Expand|Select|Wrap|Line Numbers
  1. aList[index] = valuse
Tuples do not all item assignment. As long as you assign x, y, z all at the same time, as in
Expand|Select|Wrap|Line Numbers
  1. pt = x, y, z
you can use tuples.
Expand|Select|Wrap|Line Numbers
  1. >>> d = {'a':(1,2,3),'b':(4,5,6),'c':(7,8,9)}
  2. >>> di = dict((v, k) for k, v in d.items())
  3. >>> di
  4. {(4, 5, 6): 'b', (7, 8, 9): 'c', (1, 2, 3): 'a'}
  5. >>> 
Mar 19 '07 #8

bvdet
Expert Mod 2.5K+
P: 2,851
Not if you use tuples instead of lists.
Lists let you assign into them (they are mutable) as in
Expand|Select|Wrap|Line Numbers
  1. aList[index] = valuse
Tuples do not all item assignment. As long as you assign x, y, z all at the same time, as in
Expand|Select|Wrap|Line Numbers
  1. pt = x, y, z
you can use tuples.
Expand|Select|Wrap|Line Numbers
  1. >>> d = {'a':(1,2,3),'b':(4,5,6),'c':(7,8,9)}
  2. >>> di = dict((v, k) for k, v in d.items())
  3. >>> di
  4. {(4, 5, 6): 'b', (7, 8, 9): 'c', (1, 2, 3): 'a'}
  5. >>> 
Good work Barton. I didn't think of tuples.
Expand|Select|Wrap|Line Numbers
  1. >>> d = {100: [1,0,0],102: [0,1,0],202: [0,1,1]}
  2. >>> dict(zip([tuple(x) for x in d.values()], d.keys()))
  3. {(0, 1, 0): 102, (0, 1, 1): 202, (1, 0, 0): 100}
  4. >>> 
Mar 19 '07 #9

bartonc
Expert 5K+
P: 6,596
Good work Barton. I didn't think of tuples.
Expand|Select|Wrap|Line Numbers
  1. >>> d = {100: [1,0,0],102: [0,1,0],202: [0,1,1]}
  2. >>> dict(zip([tuple(x) for x in d.values()], d.keys()))
  3. {(0, 1, 0): 102, (0, 1, 1): 202, (1, 0, 0): 100}
  4. >>> 
Take values() and keys() from a dictionary separately scares me a little. But I suppose that there's no reason for the dict to be re-orderd until a new item is add or an old one removed...
Mar 19 '07 #10

bvdet
Expert Mod 2.5K+
P: 2,851
Take values() and keys() from a dictionary separately scares me a little. But I suppose that there's no reason for the dict to be re-orderd until a new item is add or an old one removed...
That never bothered me until you mentioned it. Thinking about it - both d.keys() and d.values() are lists (ordered) so there should be no reason for the 'items' to misalign.
Mar 19 '07 #11

Post your reply

Sign in to post your reply or Sign up for a free account.