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 Collections (Retired) Lists Redux Manipulating Lists

Use .remove() and/or del to remove the string, boolean, and list members of the_list. Please advise !!!

Please advise

the_list = ["a", 2, 3, 1, False, [1, 2, 3]]

# Your code goes below here
the_list.insert(0, the_list.pop(3))

del the_list[3]

2 Answers

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

That del the_list[3] will be deleting the 3 from the list, not the False. Remember, the state of your list changes. So after you do the_list.insert(0, the_list.pop(3)), your list looks like [1, "a", 2, 3, False, [1, 2, 3]].

You're on the right track, but you probably want to make a cheat sheet for the state of the list at the end of each operation.

the_list.remove(3) is more 'Pythonic'.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Is it? I think it really depends on your use case. If the same element is in the list multiple times, .remove() will over remove the first instance of it. What if you want to leave the first but need to remove the second or third? Each feature in Python exists because each is useful in certain situations.