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

Turning a list of lists into a single list

I can get the values from my_dict but it ends up being a list of lists, if there is more than one value assigned to a single key in the dict. I can't figure out how to turn the list of lists into a single list.

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(my_dict):
    return len(my_dict)

def num_courses(my_dict):
    my_list = []
    num_values = my_dict.values()
    for i in num_values:
        my_list.append(i)
        string_list = str(my_list).split(",")
    return len(string_list)

def courses(my_dict):
    my_list = []
    num_values = my_dict.values()
    for value in num_values:
        my_list.append(value)
        my_string = str(my_list)
        my_string.split(",")
    return my_string

1 Answer

Greg Kaleka
Greg Kaleka
39,021 Points

Hi Andrew,

The append method simply adds an item to a list. So when you append a list to another list, it simply adds that list as an element in that parent list.

list_one = [1, 2, 3]
list_two = [8, 39, 9]

new_list = []

new_list.append(list_one)
# [[1, 2, 3]]
new_list.append(list_two)
# [[1, 2, 3], [8, 39, 9]]

That's not what we want, though. We want to add the individual elements of the individual lists to a parent list. Luckily, Python has another method that does exactly this: extend. It works like this:

list_one = [1, 2, 3]
list_two = [8, 39, 9]

new_list = []

new_list.extend(list_one)
# [1, 2, 3]
new_list.extend(list_two)
# [1, 2, 3, 8, 39, 9]

Perfect! So for this challenge, simply replace append with extend, and then return my_list. No need to do any splitting and returning a string.

Let me know if this makes sense!

Cheers :beers:

-Greg

Hey Greg,

Thanks for the reply. I have tried the .extend() method and it does turn the list of lists into a single list. However, the list that was inside the original list are separated strings, etc., but if there is a single value, then the it becomes a spelled out list.

Ex. my_dict = {"me":["I", "am"], "you":"you're"} Using the extend method returns: ["I","am","y","o","u","'","r","e"]

Greg Kaleka
Greg Kaleka
39,021 Points

Hi Andrew,

Sure, but in the example, the dictionary will always be {"Teacher Name": ["List of courses"]}, even if it's a single course, it will still be a list.

In your example below, you're extending a list with a string, which doesn't really make sense. A string is a list of characters, which is why it's adding the individual characters (individual list items) when you use extend. This is the place where you would want to use append.

For this challenge, though, we know the values in the dictionary will always be lists, so we can safely use extend.