0

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]]
xlmaster
  • 659
  • 7
  • 23

2 Answers2

1

There are really two problems here:

  • You are not clearing list_inner at the end of each outer loop, so it retains all the elements from all the iterations so far
  • You are appending list_inner directly to list_outer instead of appending a copy of list_inner. This means that when list_inner gets changed, the reference to it that has already been put inside list_outer gets changed as well. So you end up with the same thing repeated in list_outer.

All you need to do is change your code to copy list_inner before appending it, and also clear list_inner at the end of each outer loop:

import random

list_inner = []
list_outer = []
for i in range(5):
    for r in range(random.randint(1,10)):
        list_inner.append(r)
    list_outer.append(list_inner.copy())
    list_inner.clear()

print(list_outer)

Example output:

[[0, 1, 2, 3, 4, 5, 6, 7, 8], [0, 1, 2], [0, 1, 2, 3, 4, 5, 6], [0, 1], [0, 1, 2, 3]]
Lecdi
  • 2,189
  • 2
  • 6
  • 20
  • What is hard to digest why we need to use in your answer `list_inner.copy` in my asnwer i put it like, `list_outer.append(list(list_inner))`. It is like I tried and it worked. But why there is need to copy or make list of appended data to append it as a list ? Would be grateful if could respond this – xlmaster Apr 15 '23 at 15:57
  • 1
    @xlmaster `list_inner.copy()` and `list(list_inner)` do the same thing - make a copy of the list - although using `list_inner.copy()` is better as it makes clear what is actually being done. The reason we need to copy the list is because if we don't, we append a **reference** to `list_inner`, which will also get changed whenever `list_inner` is changed. So when we clear the list and start adding new values, the reference we have put in `list_outer` will also change in the same way. This is why you end up with multiple copies of the same thing in `list_outer`. Copying the list prevents this. – Lecdi Apr 15 '23 at 16:23
0

I resolved issue, by adding method list to inner_loop before append method and putting list inside outer loop. Here's code

import random

list_outer=[]
for i in range(5):
    list_inner = []
    for r in range(random.randint(1,5)):
        list_inner.append(r)
        print(r,list_inner)
    list_outer.append(list(list_inner))
    print('list outer',list_outer)
print(list_outer)
xlmaster
  • 659
  • 7
  • 23