FAQ: Code Challenge: Dictionaries - Values That Are Keys

This community-built FAQ covers the “Values That Are Keys” exercise from the lesson “Code Challenge: Dictionaries”.

Paths and Courses
This exercise can be found in the following Codecademy content:

FAQs on the exercise Values That Are Keys

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

2 posts were split to a new topic: My code returns an empty list?

def values_that_are_keys(my_dictionary):
  list1 = [] # Initialize list.
  for i in my_dictionary.values(): # Looping through each value in dictionary.
    if i in my_dictionary.keys(): # Comparing all keys and values that are identical using "in".
      list1.append(i) # Adding the results to list1.
  return list1

print(values_that_are_keys({1:100, 2:1, 3:4, 4:10}))
# prints [1, 4]
print(values_that_are_keys({"a":"apple", "b":"a", "c":100}))
# strong textprints ["a"]

Might be better to run loops, but I thought I should share how one can solve it in one line using list comprehensions.

# Write your values_that_are_keys function here:
def values_that_are_keys(d):
  return [x for x in d.values() if x in d.keys()]

# Uncomment these function calls to test your  function:
print(values_that_are_keys({1:100, 2:1, 3:4, 4:10}))
# should print [1, 4]

Test if it’s in d instead. Whatever dict.keys returns might not support constant-time lookup (it might, and it’s even likely, but … dict already supports that operation and is more straightforward)
In python2 dict.keys returns a list so it’s definitely wrong there. (but python2 is a bad reason to do anything, should be using python3 for most things)

Oh and this whole “is part of these values, and also part of those values” makes me think of set intersection:

return set(d.values()) & set(d.keys())


I was wondering why the following code doesn’t extract the keys and values in the dictionary and doesn’t compare them.

def values_that_are_keys(my_dictionary):
list1 =
for key, value in my_dictionary.items():
if key in value:
return list1

And the error looks like this :
Traceback (most recent call last):
File “script.py”, line 9, in
print(values_that_are_keys({1:100, 2:1, 3:4, 4:10}))
File “script.py”, line 5, in values_that_are_keys
if key in value:
TypeError: argument of type ‘int’ is not iterable

Thank you in advance!

def values_that_are_keys(my_dictionary):
  my_list = []
  for i in my_dictionary.values():
    if i in my_dictionary.keys():
      my_list += i
  return my_list

My first attempt I tried to use += i instead of .append, .append works but += does not

What is the difference between += and append here?

Is someone able to explain why this works. I assumed that it would only add the dictionary value to the list. I thought I’d have to make a duplicate that reversed the order of the lines that have ‘my_dictionary.values():’ and ‘my_dictionary.keys():’

I was surprised when it worked.

As i read it, the append is only adding one thing. But it’s added two (the value and the key).

> def values_that_are_keys(my_dictionary):
>   ls = []
>   for value in my_dictionary.values():
>     if value in my_dictionary.keys():
>       ls.append(value)
>   return ls

I just ran your code in PyCharm’s debug mode to step thru it carefully (with my own test dictionary). It did not add two (the value and the key) to the returned list for me. The list is as expected to fulfill the problem statement.

To this practice:
Create a function named values_that_are_keys that takes a dictionary named my_dictionary as a parameter. This function should return a list of all values in the dictionary that are also keys.

First solution, but the result not 100% correct

def values_that_are_keys(my_dictionary):
  return_list = []    
  keys = []              
  values = []           
for key, value in my_dictionary.items():
    for i in keys:
      for j in values:
        if i == j:


after run this function with dicts:

print(values_that_are_keys({1:100, 2:1, 3:4, 4:10}))
#I got [1, 1, 1, 4]  instead of [1, 4]
print(values_that_are_keys({"a":"apple", "b":"a", "c":100}))
#I got ['a', 'a'] instead of ['a']

My Second solution:

def values_that_are_keys(my_dictionary):
  return_list = []
  keys = [key for key in my_dictionary.keys()]
  values = [ value for value in my_dictionary.values()]

  for key in keys:
    for value in values:
      if key == value:
  return return_list      

print(values_that_are_keys({1:100, 2:1, 3:4, 4:10}))
# output is correct:   print [1, 4]
print(values_that_are_keys({"a":"apple", "b":"a", "c":100}))
# output is correct:   print ['a']

for me the ‘first solution’ compare with ‘second solution’ is the same.
can anybody give me a hint, what is problem of my first solution?

Thank you

Those codes are quite different, check your loops and indentation. There are a different number of nested loops and some iterables change size on every iteration. Perhaps some print statements could help you work it out?

I’d also be careful with a requirement to create a function that returns a list that you actually use return instead of just print.

I managed to solve this challenge using the following code:

def values_that_are_keys(d):
    lst = []
    for key, value in d.items():
        if value in list(d):
    return lst

# Uncomment these function calls to test your  function:
print(values_that_are_keys({1:100, 2:1, 3:4, 4:10}))
# should print [1, 4]
print(values_that_are_keys({"a":"apple", "b":"a", "c":100}))
# should print ["a"]

But I can’t manage to turn it into list comprehension.

lst = [d[key] for key, value in d.items() if value in list(d)]

This returns None instead of the desired answers. What am I doing wrong?

As far as I can tell your comprehension will perform as expected. What are you doing with the output? What are you return-ing?

Just a heads-up that you can skip the calls to list(dict). Checking the keys for a dictionary can be just value in dict since it only checks the keys anyway. You could also consider d[key], key and value too.

1 Like

Oh right! I didn’t realize I commented out the return as well. Also thanks for the heads up, I simplified it further. :smiley:

def values_that_are_keys(d):
        return [value for value in d.values() if value in d]
1 Like

mein solution hombres y mujeres! <3

Please tell me if what I am thinking is right
I think the code in the solution is only valid when all the values in the dictionary are unique.
Suppose my_dictionary has some keys with same value, then doing for loop of value it will match again with the same key of previous one. and append to the list. It will add same element in the list. I hope I am conveying properly.

def values_that_are_keys(my_dictionary):
  value_keys = []
  for value in my_dictionary.values():
    if value in my_dictionary: # if the value is same as prvious one where the value matched with key, it will add same value in the list.
  return value_keys

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

Here is my code. When the values could be unique or same with different keys

def values_that_are_keys(my_dictionary):
  lst = []
  for key in my_dictionary:
    for value in my_dictionary.values():
      if key == value:
  return lst

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