0

I need to fetch key from dictionary based on its value.

I want to fetch key which has value 1 in below dict.

>>> a = {'random': 0, 'fixed': 1}

>>> for k, v in a.items():
...  if 1 in v:
...   print k
...
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
TypeError: argument of type 'int' is not iterable

>>> a.items()
[('random', 0), ('fixed', 1)]

>>> a.keys()
['random', 'fixed']

>>> a.values()
[0, 1]

Can someone help me understand what am I missing here?

1 Answers1

2

You are getting error "TypeError: argument of type 'int' is not iterable". if 1 in v: is the issue.

Instead, you should write if 1 == v:.

Meloman
  • 104
  • 1
  • 7