Warm tip: This article is reproduced from stackoverflow.com, please click
list python

Deleting multiple elements from a list

发布于 2020-03-31 22:54:08

Is it possible to delete multiple elements from a list at the same time? If I want to delete elements at index 0 and 2, and try something like del somelist[0], followed by del somelist[2], the second statement will actually delete somelist[3].

I suppose I could always delete the higher numbered elements first but I'm hoping there is a better way.

Questioner
Joe Calimari
Viewed
75
935 2015-01-27 10:34

Probably not the best solution for this problem:

indices = 0, 2
somelist = [i for j, i in enumerate(somelist) if j not in indices]