Skip to content Skip to sidebar Skip to footer

Python. Filling Array In The Loop

I need to create 2d array and fill it in the loop. I know how many rows I need, but I don't know what size each row will be after the loop. In C++ I can use: vector

Solution 1:

you are right when you multiply a list of lists with a number you are getting one list and many references to the same list and when you are modifying one element you are modifying all, to solve the issue you can try using list comprehension:

aa = [[] for _ in range(3)]

or a for loop:

aa = []

for _ in range(3):
    aa.append([])

Post a Comment for "Python. Filling Array In The Loop"