Iterating Over Multidimensional Lists in Python
Let's now learn how to iterate over multidimensional lists using loops. Let's say we have the following list:
lst = [
['a', 'b', 'c'],
['d', 'e', 'f'],
['g', 'h', 'i']
]
Since this list is two-dimensional, to iterate over it you need two nested for
loops:
for sub in lst:
for el in sub:
print(el)
Given a two-dimensional list:
lst = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
Use a loop to print all the items in the list to the console.
Given a two-dimensional list:
lst = [
[2, 4, 6],
[3, 5, 7],
[9, 12, 15]
]
Use a loop to find the sum of the elements of this list.
Given a two-dimensional list:
lst = [
['a', 'b', 'c'],
['d', 'e', 'f'],
['g', 'h', 'i']
]
Use a loop to merge all the elements of a list into a string.