Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
216,013 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.