In a 2D list, each item is a list.
How do we retrieve a sublist:
How to retrieve an item in a sublist:
We will create 3 sublists of 5 items each, for that we need nested loops:
# start with an empty list
two_d_list = []
repeat = 3
while repeat > 0:
# each sublist starts as a new list
sublist = []
index = 0
while index < 5: # 5 items per list
sublist.append(index)
index += 1
# after the inner while loop, append the sublist to the main list
two_d_list.append(sublist)
repeat -= 1
print(two_d_list)
[[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]]
random
moduleWe start by importing random
.
Then we set a seed for replicable results and let’s generate a random integer:
Instructions for Short Project of the Week