Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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
231,271 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,733 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.