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

Idan shami
Idan shami
13,251 Points

what is the difference?

what is the difference between

values_list = []
teachers_course = {'Andrew Chalkley': ['jQuery Basics', 'Node.js Basics'],'KennethLove': ['Python Basics', 'Python Collections']}
for teachers in teachers_course:
    for courses in teachers_course[teachers]:
        values_list.append(courses)
    print(values_list)

and

values_list = []
teachers_course = {'Andrew Chalkley': ['jQuery Basics', 'Node.js Basics'],'KennethLove': ['Python Basics', 'Python Collections']}
for teachers in teachers_course:
    for courses in teachers_course.values():
        values_list.append(courses)
    print(values_list)

Why when I do it like this:

for courses in teachers_course.values():

its giving me the values in so many lists, but when i do this:

for courses in teachers_course[teachers]:

its giving me all the values in one list?

hope you will answer, and thank you.

Idan.

1 Answer

Steven Parker
Steven Parker
230,274 Points

Each of the values in the dictionary is a list of courses, so teachers_course.values() will be a list of all the values, and in this case that will be a list of lists.

On the other hand, inside a loop where "teachers" represents only one dictionary entry (one teacher), teachers_course[teachers] will be the single list of courses associated with that one teacher.