0

If I have a list like

a=[1,4,2]

and I create a new list in the following way

b=a

I found out that any modification to b would be applied to a as well:

b.sort()
print b
print a
b.pop(-1)
print b
print a

How can I create a copy of a list which I can modify at pleasure without affecting the original?

So, in my example, every time I print a I want to obtain the initial output [1,4,2], no matter what I do to b.

3sm1r
  • 484
  • 4
  • 16

1 Answers1

0

use b =a.copy()

then use b.sort()

assert a==b

result False

Eric Leschinski
  • 135,913
  • 89
  • 401
  • 325
sahasrara62
  • 7,680
  • 2
  • 24
  • 37