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

How do you correct this invalid list using .remove() method on last item of the list?

So far I've added states.remove($) in attempt to remove the last item. I'm currently receiving syntax error for "states.remove($) I've tried adding a space "states.remove ($) Where did I go wrong?

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

2 Answers

Scott Dunstan
Scott Dunstan
12,517 Points

remove() takes the value that you want to remove so it'd have to be:

states.remove(5) 

or if you didn't know the value of the last entry, you'd have to reference it by index using:

del states[-1]

Yikes, I thought the 5 was a dollar sign. I appreciate you taking the time to break the concept down. Thanks!

Moosa Bonomali
Moosa Bonomali
6,297 Points

If you only intend to remove the last item in the list, you can use the pop() function on the list like this;

 states = [
    'ACTIVE',
    ['red', 'green', 'blue'],
    'CANCELLED',
    'FINISHED',
    5,
 ]
 states.pop()

Thanks Moosa, appreciate the tip!