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 list.remove()

removing multiple items from list

I am trying to move the list of three colors from states in brackets & the 5 integer I know to remove the colors in the brackets it is states.remove[0:2] but having trouble to remove this and the 5 integer as I listed below

states = [ 'ACTIVE', ['red', 'green', 'blue'], 'CANCELLED', 'FINISHED', 5, ] states.remove([0:2],(5))

lists.py
states = [
    'ACTIVE',
    ['red', 'green', 'blue'],
    'CANCELLED',
    'FINISHED',
    5,
]
states.remove([0:2],(5))

1 Answer

Rich Zimmerman
Rich Zimmerman
24,063 Points

Unfortunately you can only remove 1 item at a time with the .remove() method. Depending on your implementation, you have a few options.. You can either run the remove method on the list twice, or you can create a new list

states.remove(states[1]) # removes the nested list
states.remove(5) # removes the integer 5
# OR
new_list = [item for item in states if item not in [['red', 'green','blue'], 5]]
# OR
exclude = [ ['red', 'green', 'blue'], 5]
new_list = [item for item in states if item not in exclude]