Populating Multidimensional Lists in Python
Using loops, you can populate multidimensional lists with elements.
Let's say we need to get the following two-dimensional list:
[
[1, 2, 3],
[1, 2, 3],
]
We will solve the problem by using two nested loops. The outer loop will create sublists, and the inner loop will fill these sublists with numbers:
lst = []
for i in range(0, 2):
sub_lst = [] # create a sublist
for j in range(1, 4):
sub_lst.append(j) # fill the sublist with numbers
lst.append(sub_lst)
Use loops to fill the list with the following data:
[
[1, 2, 3],
[1, 2, 3],
[1, 2, 3],
]
Two lists are given:
lst1 = []
lst2 = ['a', 'b', 'c']
Using loops, fill the first list with the elements of the second list to get the following result:
[
['a', 'b', 'c'],
['a', 'b', 'c'],
]