-1
a = [1,2,4]
b=a
b.insert(1,45)
print(a,b)

results a = [1,45,2,4] b = [1,45,2,4]

why a is changing is there any way, where b will change

1 Answers1

0

That's because you're just reassigning the same instance. You need to do this instead of b=a:

b = a.copy()
SnoopFrog
  • 684
  • 1
  • 10