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

omar talaat
PLUS
omar talaat
Courses Plus Student 171 Points

lists

'ACTIVE', ['red', 'green', 'blue'], 'CANCELLED', 'FINISHED', 5, ] states.remove(['ACTIVE' ,'CANCELLED' ,'FINISHED' ,5]) he is asking to remove the last item

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

2 Answers

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Omar;

If we look at the provided list and how the remove() method works we need to pass in an item to the remove() method for it to remove. remove() removes the first matching value it finds, unlike del() or pop() which removes things based on an index.

For example, if we have a list:

colors = ["red", "green", "blue"]

And want to remove green from the list, we can utilize remove("green") to remove that item.

colors.remove("green")

results in colors being the new list ["red", "blue"].

With all of that in mind, try the challenge again passing into remove() just the item the challenge is wanting to be removed from the provided list.

Post back if you are still stuck.

Happy coding! Ken

Aeyzechiah Vasquez
Aeyzechiah Vasquez
2,070 Points

Hi learners!

I would like to add that [List].pop() does not require an index. The pop() method automatically removes the last item of your list. It is usually used in the context of Stacks which are designed specially for retrieving the last element.

Keep on! ^^ AZ

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Aeyzechiah Vasquez -

It doesn't require an index, true, but internally it is removing the last index by default. But passing in pop(1) would, in the colors example above, also remove "green" from the list.

Ken

Aeyzechiah Vasquez
Aeyzechiah Vasquez
2,070 Points

I see, thank you!

I also see now that pop() isn't just some legacy method; pop(n) is more powerful and can remove (and return) an element from anywhere in the list, rather than only from the end. I'm still getting used to "Pythonic" thinking and these small insights are very helpful!