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

Issue with removing list items in Python.

Hello,

I am trying to remove some items from a list using .remove(). However, I get an error stating that... (see the following code and error).

new_list = [1, 6, 7, 8];
new_list.remove([7, 8]);
print(new_list);

Traceback (most recent call last):
 line 28, in <module>
    new_list.remove([7, 8]);
ValueError: list.remove(x): x not in list

Thanks!

2 Answers

The .remove function removes elements based on value inside the parenthesis, and takes exactly one argument (you cannot remove two elements at once), so new_list.remove([7, 8]) will search new_list for an element [7, 8] which is not there because 7 and 8 are present as 'individual' elements in the list. Your code will work if new_list was [1, 2, 3, 4, 5, 6, [7, 8]], i.e having a list of [7, 8] as an element in the list. You should remove 7 and 8 individually.

Ok thanks Sahil.