Nested-list processing
In Listing 1, the Python function x receives a non-empty two-dimensional list of integers and returns a list.
Considering the two-dimensional list in Table 1, what is returned when x is called with that list?
| A | B | C | D | |
|---|---|---|---|---|
| 1 | 7 | 2 | 11 | 10 |
| 2 | 11 | 1 | 2 | 1 |
| 3 | 9 | 7 | 0 | -1 |
What is the returned list?
[1, 2, 7, 9, 10][7, 2, 11, 10, 1, 2, 9][2, 0, 0, 0, 0, 0, 2, 0, 1, 1][7, 2, 11, -1, 10, 1, 2, 9, 7, 0, -1]
TipHint
range(start, stop, step)supplies integers fromstarttoward, but excluding,stop.stepdefaults to1, andrange(stop)starts at0.list(range(2, 7, 2))is[2, 4, 6];list(range(5, 1, -2))is[5, 3].- A range is empty when the step cannot reach the stop; a zero step raises
ValueError.
listcollects the supplied values in a new list.items.append(value)adds one item at the end of the list and returnsNone. Starting withitems = [8],items.append(3)changes it to[8, 3]. An appended list remains one nested item; an empty list accepts its first item the same way.