Home > Enterprise >  Python doesn`t work right. Parent list is changing by changing hereditary list
Python doesn`t work right. Parent list is changing by changing hereditary list

Time:07-20

a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26]
for i in range(1, 27):
    for j in range(1,27):
        if j!=i:
            lst = a
            print(lst)
            print(a)
            lst.remove(i)
            lst.remove(j)
print(lst)
print(a)

List 'a' is getting smaller coz i change list 'lst', wtf is this? I just started to perform codewars kata.

CodePudding user response:

By default, python creates a reference for an object. If you want to make an actual copy with new memory what you can do is:

from copy import deepcopy

lis_copy = deepcopy(lis)
  • Related