I have tried to write my error issue in reproducible manner for platform to be seen and guided. I cannot see my logic gap why this error happens.
I have a inner loop which brings new elements while scraping and appends it to list named list_inner
. Then in outer loop list named list_outer
appends that new list. But final result gives amount of members right, but elements of list list_outer are same, the last list element of list list_inner
. How can this happen? If it will be one elemented list I will understand.
import random
list_inner=[]
list_outer=[]
for i in range(5):
for r in range(random.randint(1,10)):
list_inner.append(r)
print(r)
list_outer.append(list_inner)
print(list_outer)
print(list_outer)
I am sharing for two results, as giving idea what is in real and what I was expecting. I got this result:
0
1
2
3
[[0, 1, 2, 3]]
0
1
2
3
4
[[0, 1, 2, 3, 0, 1, 2, 3, 4], [0, 1, 2, 3, 0, 1, 2, 3, 4]]
But I was expecting this result:
[[0,1,2,3],[0,1,2,3,4]]