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

mateuszwolski2
mateuszwolski2
5,504 Points

Problem .remove() doesn't see object in script.

/1.STARTING WITH THIS

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

/FIRST ATTEMPT

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

/Bummer! ValueError: list.remove(x): x not in list

/THIRD ATTEMPT

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

/ Bummer! Don't change states directly!

DON'T KNOW WHAT TO DO !

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

states.remove(['red'])

3 Answers

re-posting due to formatting in earlier post

Hello

Let think about what we are trying to do.

we want to remove element 'red' from list ['red', 'green', 'blue'] which happens to be the 2nd element in the states list. Therefore, if I do something like this for demo only:

...html i=0 for iitem in states: print('I am index {} i is states list and my value is {}'.format(i,states[i])) i += 1

this should produce this output I am index 0 i is states list and my value is ACTIVE I am index 1 i is states list and my value is ['red', 'green', 'blue'] I am index 2 i is states list and my value is CANCELLED I am index 3 i is states list and my value is FINISHED I am index 4 i is states list and my value is 5

so clearly ['red', 'green', 'blue'] is index 1 in the states list.

so if we do this, we then can access 'red'

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

states[1].remove('red')

...

Hopefully, this helps clear the concept. If this answers your question, please mark the question as answered.

Alex Diaz
Alex Diaz
4,930 Points

First of all, keep the states.remove(5), it didn't ask you to remove it. Second, it wants you to remove the ENTIRE second item in the list, aka the list of colors. Not just 'red', use this to remove it. states.remove(['red', 'green', 'blue'])

Hello

Let think about what we are trying to do.

we want to remove element 'red' from list ['red', 'green', 'blue'] which happens to be the 2nd element in the states list. Therefore, if I do something like this for demo only:

...html

i=0 for iitem in states: print('I am index {} i is states list and my value is {}'.format(i,states[i])) i += 1

this should produce this output I am index 0 i is states list and my value is ACTIVE I am index 1 i is states list and my value is ['red', 'green', 'blue'] I am index 2 i is states list and my value is CANCELLED I am index 3 i is states list and my value is FINISHED I am index 4 i is states list and my value is 5

so clearly ['red', 'green', 'blue'] is index 1 in the states list.

so if we do this, we then can access 'red'

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

states[1].remove('red')

...

Hopefully, this helps clear the concept. If this answers your question, please mark the question as answered.