-1
list1=[[1,2,3],[4,5,6],[7,8,9]]

list2=[[1,2,3],[4,5,6],[7,8,9]]

while True:

    list1.pop(0)

    list1=list2

    print(list1)
    
Sayse
  • 42,633
  • 14
  • 77
  • 146

1 Answers1

1

This hapen because you pass list2 with address to list1 then when you pop from list1 you pop from list2 too, see some iteration of your code. after three run of your while True your list1 and list2 is empty and when you pop from list1 you got an error.

>>> list1=[[1,2,3],[4,5,6],[7,8,9]]
>>> list2=[[1,2,3],[4,5,6],[7,8,9]]
>>> list1.pop(0)
[1, 2, 3]
>>> list1=list2
>>> list1.pop(0)
[1, 2, 3]
>>> list2
[[4, 5, 6], [7, 8, 9]]

If you change your code like below and use deepcopy you don't get an error:

import copy
list1=[[1,2,3],[4,5,6],[7,8,9]]
list2=[[1,2,3],[4,5,6],[7,8,9]]

while True:
    list1.pop(0)
    list1=copy.deepcopy(list2)
    print(list1)
I'mahdi
  • 23,382
  • 5
  • 22
  • 30