Oct-19-2020, 08:56 PM
Hi there!
I wanted to store my functions result in a 2d list. The function returns 5 values and has to run 100 times in a for loop.
I tried to initiate a storage list by ding this:
However, when I want to append to one of those lists inside, all of them get the value appended
The strange thing is that I am able to add an object to a list and after that, the list becomes appendable
I solved the problem in a far less elegant way, by initiating a list like this
However, this is not flexible and is time consuming to adjust if the problem changes.
Is there a more elegant/simple/flexible way to solve this problem?
I wanted to store my functions result in a 2d list. The function returns 5 values and has to run 100 times in a for loop.
I tried to initiate a storage list by ding this:
list1 = [[]]*5which returns list1 = [[], [], [], [], []]
However, when I want to append to one of those lists inside, all of them get the value appended
list1[0].append(3)Would return: [[3], [3], [3], [3], [3]]
The strange thing is that I am able to add an object to a list and after that, the list becomes appendable
list1[0] = [1] list1[0].append(3)returns [[1, 3], [], [], [], []]
I solved the problem in a far less elegant way, by initiating a list like this
list1 = [[0],[1],[2],[3],[4]]and after appending, removing the first values.
However, this is not flexible and is time consuming to adjust if the problem changes.
Is there a more elegant/simple/flexible way to solve this problem?