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

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

Please advise

lists.py
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
STAFF
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.