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()

Not sure how to complete this excercise...

It says "Ugh, I made this list and now it has some invalid pieces in it. Maybe you can help me clean it up. Use the .remove() method to remove the last item from the list, please."

lists.py
states = [
    'ACTIVE',
    ['red', 'green', 'blue'],
    'CANCELLED',
    'FINISHED',
    5,
]

2 Answers

states.remove(5)

Hannu Shemeikka
Hannu Shemeikka
16,799 Points

Hi,

The task requires you to remove the last item from the list. This can be achieved in several ways and here's one:

states = [
    'ACTIVE',
    ['red', 'green', 'blue'],
    'CANCELLED',
    'FINISHED',
    5,
]
# remove-method requires an integer which tells which index-position is going to be removed from the list
states.remove(len(states))  # Uses the len-function to get the number of items in the list
# or 
states.remove(4)

This should work.

Awesome, thanks!

Hannu Shemeikka
Hannu Shemeikka
16,799 Points

Oops..

I made a small mistake here. I forgot that remove-method doesn't take index-value but the actual value instead.

From the docs "Remove the first item from the list whose value is x. It is an error if there is no such item."

So the code should be

states.remove(5)