Learn
Finally, this exercise shows how to make use of a single list that contains multiple lists and how to use them in a function.
list_of_lists = [[1, 2, 3], [4, 5, 6]] for lst in list_of_lists: for item in lst: print item
- In the example above, we first create a list containing two items, each of which is a list of numbers.
- Then, we iterate through our outer list.
- For each of the two inner lists (as
lst
), we iterate through the numbers (asitem
) and print them out.
We end up printing out:
1 2 3 4 5 6
Instructions
1.
Create a function called flatten
that takes a single list and concatenates all the sublists that are part of it into a single list.
- On line 3, define a function called
flatten
with one argument calledlists
. - Make a new, empty list called
results
. - Iterate through
lists
. Call the looping variablenumbers
. - Iterate through
numbers
. - For each number,
.append()
it toresults
. - Finally,
return results
from your function.
Take this course for free
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.