0

hello guys i have a question about ""python"" please help me I have 2 list for example a and b when I cast list a to b and then I change elements of list b both list changing but I want not change list a(prime list) change at all

code is here in python:
a = [4,7]
b = []
b = a
b[1] = 0
print(a)
#####
output is:
[4, 0]

enter image description here

1 Answers1

0

This is an optimization issue as python does not create a new memory location for b if b=a. So essentially they are the same object and reside at the same location. To create a copy of a and store it in b, you can b = a[:] or b = a.copy().

Saatvik Ramani
  • 392
  • 3
  • 8