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
  • 520
  • 4
  • 19

1 Answers1

0

use b =a.copy()

then use b.sort()

assert a==b

result False

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
sahasrara62
  • 10,069
  • 3
  • 29
  • 44