Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
208,481 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,732 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.