Very well done! Now, let's tackle a classic programming problem. Suppose we need to transpose a rectangular nested list, i.e. change its rows into columns:
How can we do that in Python? Take a look:
numbers_list = [
[1, 4, 7, 10],
[2, 5, 8, 11],
[3, 6, 9, 12]
list_to_return = 
for i in range(len(numbers_list)):
new_row = 
for j in range(len(numbers_list)):
Note what happens:
- In the outer loop (
i), we iterate over the length of the first nested list, i.e. over the "columns".
- Inside the loop, we create a variable for a new row (an empty list).
- In the inner loop (
j), we iterate over the outer list's length, i.e. over the "rows".
- Inside the inner loop, we add the
i-th element from each nested list:
- We add the new row to the new list.