How can ı change this code to make list like [[0],[0,1],[0,1,2]]
a=[]
lst = 3
element = 1
for i in range(lst):
a.append([0,1,2] * element)
print(a)
output = [[0, 1, 2], [0, 1, 2], [0, 1, 2]]
The change that works while changing the least amount of code:
a.append( [0,1,2][:i+1]* element) # only change this line
A better approach would probably be to use a list comprehension. The whole code will then be something like:
n = 3
my_list = [0,1,2]
output = [my_list[:i] for i in range(1,n+1)]
print(output)
output:
[[0], [0, 1], [0, 1, 2]]
3,4 correspond to that output? 3 will be the number of sublists I think, but what does an input of 4 mean?