Python Python Basics (2015) Python Data Types Lists

How to remove more than one value from a list ?

Is there a different method to remove more than one item from a list

If you try doing that for example : mylist.remove(4,5)

It returns Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: remove() takes exactly one argument (2 given)

Is there a different solution to this ?

2 Answers

Steven Parker
Steven Parker
176,676 Points

:point_right: You probably want to use slice.

For example, to remove elements 4 and 5:

mylist = mylist[:4] + mylist[6:]

Or to remove the last 10 elements from a list:

mylist = mylist[:-10]
Kourosh Raeen
Kourosh Raeen
23,710 Points

Hi Lionel - If you want to remove contiguous indexes you can try this:

mylist[4:6] = []

If the indexes are not contiguous you can try something like this:

mylist = [i for j, i in enumerate(mylist) if j not in [0, 6, 8]]

which removes the items at indexes 0, 6, and 8.