0

given the following list:

l=[1,2,3]
l2=l1[:]
l2[0]=10
print(l)

#[10,2,3]

How do I copy the original list and make changes on the copied list without changing the original list (without using copy.deepcopy(x))?

And another question,

if I can create l2 by assigning l1 to l2 why does the copy() function exists? does it do anything differently?

l2=l1
l2=l1.copy()

The things that I tried to do are shown above.

Hung
  • 155
  • 10
  • 1
    [This answer](https://stackoverflow.com/questions/2612802/how-do-i-clone-a-list-so-that-it-doesnt-change-unexpectedly-after-assignment) might solve the problem! – Hung Feb 03 '23 at 14:19
  • 2
    You are using list slicing. That should not be a problem in your case. Can you please check your code again? – Lax_Sam Feb 03 '23 at 14:21
  • Why do you want to make a copy of a list without using a copy function, exactly? – B Remmelzwaal Feb 03 '23 at 14:25
  • Your code (if you change `l` to `l1` or otherwise) works exactly as you want. Try it again, please. If it still doesn't work, fix variable naming, please – Yevhen Kuzmovych Feb 03 '23 at 14:26

0 Answers0