-2

I have a list of dictionaries and I am trying to get the dictionary key by its value:

list1 = [{'a':1,'b':2,'c':3},
         {'d':4,'e':5,'f':6},
         {'g':7,'h':8,'i':9}]

Here I am trying to get the dictionary key if the value 5 exists in the list of dictionaries.

Zero Piraeus
  • 56,143
  • 27
  • 150
  • 160
Surya G
  • 25
  • 7
  • 2
    What result are you expecting if multiple keys have the target value? Does it differ if the keys are in different dictionaries? – Peter DeGlopper Mar 07 '17 at 06:07
  • 1
    Possible duplicate of : [Get key by value in dictionary](http://stackoverflow.com/questions/8023306/get-key-by-value-in-dictionary) – Mp0int Mar 07 '17 at 06:42
  • @FallenAngel that question is asking about a single dictionary, not a list of dictionaries. – Zero Piraeus Mar 07 '17 at 06:49
  • @ZeroPiraeus so? Go through the list and look up in each dictionary? – SQB Mar 07 '17 at 09:50
  • Possible duplicate of [Get key by value in dictionary](http://stackoverflow.com/questions/8023306/get-key-by-value-in-dictionary) – SQB Mar 07 '17 at 09:51

3 Answers3

0

Your question is ambiguous, as pointed out by Peter DeGlopper:

What result are you expecting if multiple keys have the target value? Does it differ if the keys are in different dictionaries?

However, if for example you want a list of 2-tuples (i, k) – where i is the index (in the list) of the dictionary where the value is found, and k is the key associated with the value – the following works:

def index_keys(data, value):
    return [(i, k) for i, d in enumerate(data) for k, v in d.items() if v == value]

Examples:

>>> list1 = [
...     {'a':1,'b':2,'c':3},
...     {'d':4,'e':5,'f':6},
...     {'g':7,'h':8,'i':9}
... ]
>>> index_keys(list1, 5)
[(1, 'e')]
>>> list1[1]['e'] == 5
True

>>> list2 = [
...     {'a': 1, 'b': 2, 'c': 3, 'd': 4},
...     {'w': 2, 'x': 4, 'y': 6, 'z': 8},
...     {'a': 6, 'b': 6, 'y': 6, 'z': 8}
... ]
>>> index_keys(list2, 2)
[(0, 'b'), (1, 'w')]
>>> index_keys(list2, 8)
[(1, 'z'), (2, 'z')]
>>> index_keys(list2, 6)
[(1, 'y'), (2, 'a'), (2, 'y'), (2, 'b')]
Community
  • 1
  • 1
Zero Piraeus
  • 56,143
  • 27
  • 150
  • 160
  • Now my current scenario is to get the value of a particular key from the dictionary where it has different keys. – Surya G Mar 09 '17 at 10:41
0

If the values are distinct over all the dictionaries as they are here, you can build a reverse lookup dictionary:

reverselookup = {}
for d in list1:
    reverselookup.update({v: k for k, v in d.items()})

You can then use this to find the keys corresponding to a value by indexing the dictionary:

>>> reverselookup[5]
'e'

If the values are not distinct, but the keys are, you can build a list of corresponding keys instead.

import collections
reverselookup = collections.defaultdict(list)
for d in list1:
    for v, k in d.items():
        reverselookup[v].append(k)
chthonicdaemon
  • 19,180
  • 2
  • 52
  • 66
-1
for dic in list1:
    for key in dic:
        if dic[key] == search_value:
            print key
Bijoy
  • 1,131
  • 1
  • 12
  • 23
  • 5
    Please don't name a variable `list`. It's even worse when the variable named `list` is actually a `dict`. – ShadowRanger Mar 07 '17 at 06:17
  • 2
    This isn't entirely wrong, but it's bad style. #1, don't use reserved words like `list` as variable names, that can collide with built in methods in confusing ways. #2, the contents of `list1` are dicts rather than lists, so `for list in list1` is both misleading and mildly unsafe - only mildly because direct use of the `list` constructor is rare. It also assumes child dicts of exactly one comparable value, which may or may not be accurate. – Peter DeGlopper Mar 07 '17 at 06:23