My_dictionary.values() vs. my_dictionary

Hello, I’m trying to understand the difference between accessing values using .values versus using the dictionary name directly. Below I will provide my own code versus codecademy’s sotution. Both of them work and I just want to know why. here is the link to the challenge (it’s challenge number 4): https://www.codecademy.com/courses/learn-python-3/articles/python-code-challenges-dictionaries.

My code:

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

Codecademy’s solution:

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

Thank you for all your help in advance!

if value in my_dictionary.keys()
and
if value in my_dictionary
do the same thing,
they both check whether what’s stored in the variable called value is one of the keys of the dictionary my_dictionary.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.