Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Collections (Retired) Lists Redux Manipulating Lists

Warren Chisasa
Warren Chisasa
3,731 Points

The use of '.remove()' and del function

I asked this question last time but I guess I was vague because my code was undocumented. But I trying to remove the string, the boolean and the list of numbers at the last index, [1,2,3]. However, the code isn't still working. Any help please.

lists.py
the_list = ["a", 2, 3, 1, False, [1, 2, 3]]
the_list.pop(0)#pop out the string 'a'
the_list.insert(0, the_list.pop(2))#move 1 to position zero
the_list.remove('a')#remove the string
the_list.remove(False)#Remove boolean
del the_list [-1]#Remove the list members [1,2,3] at the last index

# Your code goes below here

1 Answer

David Bath
David Bath
25,939 Points

Are you just trying to pass the challenge? If that's the case then I don't know why your first line is there (popping out "a") - you do that as part of the next line, where you pop and re-insert the "a" in one step (except you have the index wrong).

Warren Chisasa
Warren Chisasa
3,731 Points

Hi David, Yes, that's the order of the task. I should first pop the string then I should remove it from the list. But the problem is that the code is wrong, I want to find out if that's how I should use the pop and remove function.

Thanks!

David Bath
David Bath
25,939 Points

Sorry for the delayed response... The instructions for the first step is: "Move the 1 to position 0. You can do this in one step with .pop() and .insert()." You are doing that with your second line:

the_list.insert(0, the_list.pop(2))

Except that the index you pass to pop should be 3. Also, your first line shouldn't pop out the "a" string because that is part of step 2 of the challenge, and you are doing that with your "remove" statement.