445,885 Members | 1,499 Online
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
10 Replies

 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   >>> d = {'a':1, 'b':2, 'c':3} >>> di = dict((v, k) for k, v in d.items()) >>> di {1: 'a', 2: 'b', 3: 'c'} >>>  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 Sample>>> d = {100: [1,0,0],102: [0,1,0],202: [0,1,1]} >>> di = dict((v, k) for k, v in d.items())   Traceback (most recent call last):   File "", line 1, in -toplevel-     di = dict((v, k) for k, v in d.items()) TypeError: list objects are unhashable >>>  I am getting the above error when I applied for the example I mentioned -PSB Mar 18 '07 #4

 Expert Mod 2.5K+ P: 2,851 Expand|Select|Wrap|Line Numbers Sample>>> d = {100: [1,0,0],102: [0,1,0],202: [0,1,1]} >>> di = dict((v, k) for k, v in d.items())   Traceback (most recent call last):   File "", line 1, in -toplevel-     di = dict((v, k) for k, v in d.items()) TypeError: list objects are unhashable >>>  I am getting the above error when I applied for the example I mentioned -PSB Try this: Expand|Select|Wrap|Line Numbers >>> dict(zip([str(x) for x in d.values()], d.keys())) {'[0, 1, 1]': 202, '[1, 0, 0]': 100, '[0, 1, 0]': 102} >>>  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 if  [0,1,0] in d.values():     print "found "   Mar 18 '07 #6

 100+ P: 440 Try this: Expand|Select|Wrap|Line Numbers >>> dict(zip([str(x) for x in d.values()], d.keys())) {'[0, 1, 1]': 202, '[1, 0, 0]': 100, '[0, 1, 0]': 102} >>>  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

 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 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 pt = x, y, z you can use tuples. Expand|Select|Wrap|Line Numbers >>> d = {'a':(1,2,3),'b':(4,5,6),'c':(7,8,9)} >>> di = dict((v, k) for k, v in d.items()) >>> di {(4, 5, 6): 'b', (7, 8, 9): 'c', (1, 2, 3): 'a'} >>>  Mar 19 '07 #8

 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 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 pt = x, y, z you can use tuples. Expand|Select|Wrap|Line Numbers >>> d = {'a':(1,2,3),'b':(4,5,6),'c':(7,8,9)} >>> di = dict((v, k) for k, v in d.items()) >>> di {(4, 5, 6): 'b', (7, 8, 9): 'c', (1, 2, 3): 'a'} >>>  Good work Barton. I didn't think of tuples. Expand|Select|Wrap|Line Numbers >>> d = {100: [1,0,0],102: [0,1,0],202: [0,1,1]} >>> dict(zip([tuple(x) for x in d.values()], d.keys())) {(0, 1, 0): 102, (0, 1, 1): 202, (1, 0, 0): 100} >>>  Mar 19 '07 #9

 Expert 5K+ P: 6,596 Good work Barton. I didn't think of tuples. Expand|Select|Wrap|Line Numbers >>> d = {100: [1,0,0],102: [0,1,0],202: [0,1,1]} >>> dict(zip([tuple(x) for x in d.values()], d.keys())) {(0, 1, 0): 102, (0, 1, 1): 202, (1, 0, 0): 100} >>>  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

 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