Flattening a List

The questions below are due on Thursday June 17, 2021; 11:00:00 AM.
 
You are not logged in.

Please Log In for full access to the web site.
Note that this link will take you to an external site (https://shimmer.mit.edu) to authenticate, and then you will be redirected back to this page.

Write a program that does a top-level "flatten" of a list. That is, given a list whose elements are all sequences themselves (strings, lists, or tuples), print a list which contains all the elements of those sequences, in the same order.

The first line of your program should set up a variable l to hold the input list. For example:

l = [(1, 2, 3), ["a", "b", "c"], [0], "hi", ["more", "examples"]]

Your program should print the flattened result:

[1, 2, 3, "a", "b", "c", 0, "h", "i", "more", "examples"]

Notice that the elements of the input list's elements are not flattened: "more" does not become "m", "o", "r", ....1

Once you've tested your code by hand, upload your file for testing:

  No file selected


 
Footnotes

1In a sense, we're only asking you flatten one "level" of the list. Later in this course, we'll learn a tool, recursion, which could be used to flatten an arbitrary number of levels. (click to return to text)