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

Jonathan M. Stomber
Jonathan M. Stomber
1,702 Points

Totally stuck on .remove LIST with no good online answers.. Please help!

Can someone help me get past this one tiny hurdle? Take a look at my code and tell me what you think?

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

4 Answers

Problem with index:

To remove last item you must remove 'len(LIST)-1' index element.

TO remove second element you must remove second-1=>List[1] element.

see the code for more detail.

states = [
    'ACTIVE',
    ['red', 'green', 'blue'],
    'CANCELLED',
    'FINISHED',
    5,
]
states.remove(states[len(states) - 1])
states.remove(states[1])
Jonathan M. Stomber
Jonathan M. Stomber
1,702 Points

Thank you so much!!! I literally spent hours working on this, yet it is so simple! Maybe I just needed to step back from it for a bit. Anyways, THANK YOU so much! you are a life saver!

Jonathan M. Stomber
Jonathan M. Stomber
1,702 Points

If you don't mind, can you explain some of the logic behind the answer?

we were told to remove the 2nd and last element of the list.

to remove the second element we remove the element at index 1, as indexing starts from 0.

for last element, we calculate the total length of the list using len(). and then subtracting one from it. so len(list_name)-1 index.

finally we use remove function to remove the item specified in the argument-list of remove function.