0

I have a dictionary like this

odict([(1, {'media_one': '000121'}), (2, {'media_two': '201984'}), (3, {'media_three': '301984'})])

I want to check if the key media_two or media_one or media_three is exists in this dictionary or not. If exists do something else do nothing. How can we check

sandeep
  • 3,061
  • 11
  • 35
  • 54
  • odict is an ordered dict. I have created like this from odict import odict as OrderedDict media_dict = OrderedDict(). then media_dict.update({ 1:{'media_one':'000121'}, 2:{'media_two':'201984'}, 3:{'media_three':'301984'}, }) – sandeep Feb 06 '13 at 08:02

2 Answers2

2
>>> odict = OrderedDict([(1, {'media_one': '000121'}), (2, {'media_two': '201984'}), (3, {'media_three': '301984'})])
>>> any("media_one" in item for item in odict.values())
True

This checks that one of the keys is present. To check for all of the keys:

>>> all(any(key in item for item in odict.values()) 
...                     for key in ("media_one", "media_two", "media_three"))
True
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
0

(lambda d: ["media_one" in m for m in d.values()])(odict)

Output

[True, False, False]

Mirage
  • 30,868
  • 62
  • 166
  • 261