t92
t92

Reputation: 13

Why Am I Getting A Type Error When Running This Function?

I'm trying to create a function where it returns a list of all values in the dictionary that are also keys. However I keep getting 'TypeError: argument of type 'int' is not iterable' printed in the Terminal. Why is this code not working?

def values_that_are_keys(my_dictionary):
 for element, numbers in my_dictionary.items():
  if numbers in element:
      print(numbers)

values_that_are_keys({1:100, 2:1, 3:4, 4:10})

Upvotes: 0

Views: 91

Answers (1)

khelwood
khelwood

Reputation: 59185

Because element is an integer. You can't check if something is in an integer.

If you want to check if numbers (which, incidentally, should not be a plural) is a key in my_dictionary, you want

if numbers in my_dictionary:
    ...

From https://docs.python.org/3/library/stdtypes.html#typesmapping:

key in d

Return True if d has a key key, else False.

Upvotes: 2

Related Questions