-1

I am two list with same value [6,1,2,3,4].After that i'am changing the first value of list_x as 0.But its also changing the list_y.

list_x = list_y = [6,1,2,3,4]

list_x[0] = 0 

print (list_y)

//output:[0,1,2,3,4]

is there any way to change the list_x alone?i need to change the value of only one list how can i do this?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Allwin_6
  • 1
  • 1

1 Answers1

-1

Try using slicing:

list_x = [6, 1, 2, 3, 4]

list_y = list_x[:]  # Creates a copy of list_x

list_x[0] = 0

print(list_x)  #Output: [0, 1, 2, 3, 4]

print(list_y)  #Output: [6, 1, 2, 3, 4]
ouroboros1
  • 9,113
  • 3
  • 7
  • 26
  • list_a = [6, 1, 2, 3, 4] list_b = list(list_a) print("Before") print(list_a) print(list_b) list_a[0] = 0 print("After") print(list_a) print(list_b) – abhimanyu dilliwal Aug 22 '23 at 07:55