1
a = {'name1':'tom'}
b = {'name2':'harry'}
c = {'name3':'peter'}

For a given key, I would like to get the name of the dictionary contains it.
Example: If i give 'name2', I would like to get b as the result
Thanks

Suren
  • 207
  • 2
  • 11

5 Answers5

4

You can use next to return the next value in a sequence, and filter it based on some criteria:

key = 'name2'
found = next(d for d in (a, b, c)
             if key in d)
Peter Wood
  • 23,859
  • 5
  • 60
  • 99
  • 1
    This answer has been enlightenment for me... That said, `next` supports a default result via an optional argument, that imo in this case can be used fruitfully: `found = next((d for d in (a, b, c) if key in d),None)` followed by a conditional block `if found: ...` – gboffi Mar 22 '16 at 14:18
  • The OP doesn't say what they want to do if the key doesn't exist. It's often [easier to ask forgiveness than permission](https://docs.python.org/2/glossary.html#term-eafp). – Peter Wood Mar 22 '16 at 14:33
2
a = {'name1':'tom'}
b = {'name2':'harry'}
c = {'name3':'peter'}

name = 'name2'

for k, v in locals().copy().items():
    if isinstance(v, dict):
        if name in v:
            print('"{}" contains id dict "{}"'.format(name, k))
# "name2" contains in dict "b"

But usually you shouldn't do it. Create dict of dicts and iterate through it:

ds = {
    'a': {'name1':'tom'},
    'b': {'name2':'harry'},
    'c': {'name3':'peter'},
}

name = 'name2'

for k, v in ds.items():
    if name in v:
        print('"{}" contains id dict "{}"'.format(name, k))
# "name2" contains in dict "b"
Mikhail Gerasimov
  • 36,989
  • 16
  • 116
  • 159
1

Since you said you would like to get b back as a variable, one way I can think of doing this is making a list of your dictionaries and enumerating through them.

a = {'name1':'tom'}
b = {'name2':'harry'}
c = {'name3':'peter'}
name = 'name2'

dicts = list(a,b,c)
required_dic = findDict(dicts)


def findDict(dicts):    
    for obj in dicts:
        if "name2" in obj:
            return obj 

but as @germn said, a better idea would be to create a nested dictionary.

Krishh
  • 602
  • 1
  • 8
  • 25
  • 1
    `dict.has_key(key)` [is deprecated](https://docs.python.org/2/library/stdtypes.html#dict.has_key) in favour of `key in dict`. See [this question](https://stackoverflow.com/questions/1323410/has-key-or-in) – Peter Wood Mar 22 '16 at 12:48
  • @PeterWood Thanks! I've edited it. – Krishh Mar 22 '16 at 12:51
1

Let's say that you put references to your dictionaries in an iterable

l = [da, db, dc, ...]

that you initialize the reference to one of your dictionaries to an invalid value

d = None

to find which dictionary has word as a key it's simply

for _ in l:
    if word in _:
        d = _
        break

at this point, either you didn't find word in any of your dicts and d is hence still equal to None, or you've found the dictionary containing word and you can do everything you want to it

if d:
    ...
gboffi
  • 22,939
  • 8
  • 54
  • 85
0

You may create a mapping dict, which maps the key to variable binding:

{"name1" : a, "name2" : b, "name3" : c}

linusg
  • 6,289
  • 4
  • 28
  • 78
  • They contain a huge number of nested keys, so mapping is not possible for all the keys I guess – Suren Mar 22 '16 at 12:32