my .pop() function affects multiple lists, do i need to store the first list in another way?

Question:

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?

Asked By: vincent verster

||

Answers:

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

new = old.copy()
Answered By: to_data
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.