-1

I try to save a new list with the variables of an old list, and then .pop() the last item from the new list without affecting the old list.

new = ['Bram', 'Glenn', 'Menno']
old = ['Glenn', 'Menno', 'Eefke Maria']

print('new1', new)
print('old1', old)
new = old
print('new2', new)
print('old2', old)
new.pop()
print('new3', new)
print('old3', old)

this gives the output:

new1 ['Bram', 'Glenn', 'Menno']
old1 ['Glenn', 'Menno', 'Eefke Maria']
new2 ['Glenn', 'Menno', 'Eefke Maria']
old2 ['Glenn', 'Menno', 'Eefke Maria']
new3 ['Glenn', 'Menno']
old3 ['Glenn', 'Menno']

But I would like the output:

new1 ['Bram', 'Glenn', 'Menno']
old1 ['Glenn', 'Menno', 'Eefke Maria']
new2 ['Glenn', 'Menno', 'Eefke Maria']
old2 ['Glenn', 'Menno', 'Eefke Maria']
new3 ['Glenn', 'Menno']
old3 ['Glenn', 'Menno', 'Eefke Maria']

Is it the way I store my list variables?

1 Answers1

2

you need to create a copy for your list check this answer for more info https://stackoverflow.com/a/2612815/18317391:

new = old.copy()
Mouad Slimane
  • 913
  • 3
  • 12