0

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})
1
  • 1
    Because element is an integer. You can't check if something is in an integer. Commented Mar 4, 2021 at 13:31

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

But won’t this code check if ‘numbers’ is in both the key AND value of the dictionary?
@t92 No, it won't. It will check if it is a key. See docs.python.org/3/library/stdtypes.html#typesmapping

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.