1 year ago

#236513

test-img

Ayush

using python `del` on a list index deletes the corresponding item in other identical lists

I'm new to using python's del keyword, and I ran into a problem.

I define two lists that have the same elements like this:

>>> list1 = [1, 2, 3, 4, 5, 6]
>>> list2 = list1

I want to delete an element at a specific index in only one of the lists. However, when using python's del keyword, it deletes the same element in the other list. For example,

>>> del list1[3]
>>> list2
[1, 2, 3, 5, 6]

Because list1 is going to be a dynamic input, I have to define list2 to be equal to list1 instead of writing out the list. I also want to be able to delete at an index, and using python's del keyword is the only way I found to do that.

Is there any other method I can use to delete elements by index? Thanks!

python-3.x

list

del

0 Answers

Your Answer

Accepted video resources