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
  1. In the example above, we first create a list containing two items, each of which is a list of numbers.
  2. Then, we iterate through our outer list.
  3. For each of the two inner lists (as lst), we iterate through the numbers (as item) 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 called lists.
  • Make a new, empty list called results.
  • Iterate through lists. Call the looping variable numbers.
  • Iterate through numbers.
  • For each number, .append() it to results.
  • Finally, return results from your function.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?