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 () 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 () below!
Agree with a comment or answer? Like () to up-vote the contribution!
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:
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:
list1.append(key)
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
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():
keys.append(key)
values.append(value)
for i in keys:
for j in values:
if i == j:
return_list.append(i)
else:
continue
print(return_list)
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_list.append(key)
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?
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):
lst.append(d[key])
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.
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.
value_keys.append(value)
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:
lst.append(key)
break
return lst
#print(values_that_are_keys({1:100, 2:1, 3:4, 4:10}))