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 Teacher Stats

Alex Diaz
Alex Diaz
4,930 Points

Need to make a single list

So I know that if I do

for course in arg.values():
    print(value)

I'll get all the course names but they'll be in []. I'm trying to make an empty list and append all the courses into that list but i'm having trouble doing that, thanks!

teachers.py
# The dictionary will look something like:
# {'Andrew Chalkley': ['jQuery Basics', 'Node.js Basics'],
#  'Kenneth Love': ['Python Basics', 'Python Collections']}
#
# Each key will be a Teacher and the value will be a list of courses.
#
# Your code goes below here.
def num_teachers(arg):
    return len(arg.values())

def num_courses(arg):
    total = 0
    for courses_list in arg.values():
        courses_for_teacher = len(courses_list)  # obtain number of courses for teacher
        total += courses_for_teacher  # add to total
    return total

def courses(arg):
    list_of_courses = []
    for course in arg.values():
        list_of_courses.append(course)
    return list_of_courses

1 Answer

After you run this script, your function will return (if you pass in the example courses):

[['jQuery Basics', 'Node.js Basics'], ['Python Basics', 'Python Collections']]

And, of course you want it to not have lists inside of lists but just one list containing them all.

You can do that by using the extend function instead of the append function.

Example in Python Shell:

>>> my_list = ['a', 'b', 'c']
>>> my_list
['a', 'b', 'c']
>>> my_list.extend(['d', 'e', 'f'])
>>> my_list
['a', 'b', 'c', 'd', 'e', 'f']

As you see, the extend function adds all of the elements inside one list then appends all of those elements to the other list.

I hope you understand.

~Alex

If you need more hints, please ask below :smile:

Alex Diaz
Alex Diaz
4,930 Points

Thank you so much man! I understand it a lot better now!

No probs!