Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Collections (Retired) Dictionaries Teacher Stats

Travis Bailey
Travis Bailey
13,675 Points

What's the difference using my_dict.values() over my_dict[value] ?

I've been refreshing my Python knowledge and dictionaries have always been a pain for me. I was a little puzzled during this challenge when my code failed while trying to use the my_dict.values() method over calling values in the loop with my_dict[value]. Can anyone explain the difference to me?

This is the loop that failed:

for item in my_dict:
  if len(my_dict.values()) > max_count:
   max_count = len(my_dict.values())
   cur_max_count = item

This is the loop that succeeded:

for item in my_dict:
  if len(my_dict[item]) > max_count:
    max_count = len(my_dict.[item])
    cur_max_count = item

Below is the entire code for reference. The below passed.

teachers.py
# The dictionary will be something like:
# {'Jason Seifer': ['Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations'],
#  'Kenneth Love': ['Python Basics', 'Python Collections']}
#
# Often, it's a good idea to hold onto a max_count variable.
# Update it when you find a teacher with more classes than
# the current count. Better hold onto the teacher name somewhere
# too!
#
# Your code goes below here.
def most_classes(my_dict):
  max_count = 0
  cur_max_count = ''
  for item in my_dict:
    if len(my_dict[item]) > max_count:
      max_count = len(my_dict[item])
      cur_max_count = item
  return cur_max_count

Travis,

The difference between my_dict.values() and my_dict[value] is quite different. Lets break them down.

I will use my_dict = {'one': 1, 'two': 2, 'three': 3} in this example.

my_dict.values() is a method of a dictionary and it returns all the values of all the keys. So

my_dict.values() would return [1, 2, 3] which are all the 'values' in this dictionary. Now to return the value of each key we would iterate over the dictionary and ask for the value of that key like:

for item in my_dict: print(my_dict[item])

which will print each value one by one

my_dict['one'] returns 1 my_dict['two'] returns 2 my_dict['three'] returns 3

I hope this helps explain the difference between the two statements.

Ron

2 Answers

Travis,

The difference between my_dict.values() and my_dict[value] is quite different. Lets break them down.

I will use my_dict = {'one': 1, 'two': 2, 'three': 3} in this example.

my_dict.values() is a method of a dictionary and it returns all the values of all the keys. So

my_dict.values() would return [1, 2, 3] which are all the 'values' in this dictionary. Now to return the value of each key we would iterate over the dictionary and ask for the value of that key like:

for item in my_dict: print(my_dict[item])

which will print each value one by one

my_dict['one'] returns 1 my_dict['two'] returns 2 my_dict['three'] returns 3

I hope this helps explain the difference between the two statements.

Ron

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Yeah, like Ron Fisher said (you should make it an answer so you can get credit for it), .values() returns all of the values in a dict, ['key'] only returns the value for that key.