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 (2016, retired 2019) Dictionaries Dictionary Iteration

Hussein Amr
Hussein Amr
2,461 Points

Didn't get how printing the minutes of the courses works

for course in course_minutes:
      print(course_minutes[course])

How does that print the values exactly?

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

Let's look at the two statements. First:

for course in course_minutes:

Says:

  • treat course_minutes as an iterable object
  • take each item from the iterable and assign it to the variable course
  • then run the loop code using this new value of course

When a dict is used in the context of an iterable, it will respond with a list of the dictionary keys. So course will contain the various course names.

      print(course_minutes[course])

Says:

  • use the course defined by the for loop as the key to the dict course_minutes
  • look up the value, and print it

Hope this helps. Post back if you need more help. Good Luck!!

Idan shami
Idan shami
13,251 Points

I don't understand why if the course defined as a key it means to look up the value, why it is saying that, i just cannot understand it.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 Points

It is the square brackets around course that designates it semantically as a dict key In dict notation, an item is looked up using the form some_dict[key]. Otherwise course is just a string object.