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 (2016, retired 2019) Lists Removing items from a list

John Smith
seal-mask
.a{fill-rule:evenodd;}techdegree
John Smith
Python Web Development Techdegree Student 1,052 Points

What's the simplest way to solve this using insert and pop method?

Please help!

lists.py
messy_list = ["a", 2, 3, 1, False, [1, 2, 3]]







# Your code goes below here

messy_list.insert(0, messy_list.pop(3))
messy_list.remove(str("a"))
messy_list.remove(False)
John Smith
seal-mask
.a{fill-rule:evenodd;}techdegree
John Smith
Python Web Development Techdegree Student 1,052 Points

It keep saying I can't just simply add the 1, but I have to move the one to the front of the list.

typo *messy_list.insert(0, 1)

2 Answers

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hi Ruddy, I think you use the simplest way to solve this challenge already :thumbsup: . But you forget to remove the list inside the list ... This one should work:

messy_list.remove("a")
messy_list.remove(False)
messy_list.remove([1, 2, 3])

Another way would be to use a for loop that checks the type of every list item and removes it if it is not an integer. The code could look like this:

# use [:] to create a copy of the list to be able to modify and iterate simultaneously
# otherwise you will delete an item and mess with up the iteration
# use type method, because otherwise you will compare a reference to a item and not item itself
for item in messy_list[:]: 
    if type(item) != int:
        messy_list.remove(item)

Does it make sense?

John Smith
seal-mask
.a{fill-rule:evenodd;}techdegree
John Smith
Python Web Development Techdegree Student 1,052 Points

It does make sense, what doesn't make sense is that it keeps returning >>>"You need to move the 1 to the front of the list."

The challenge is:

Alright, my list is messy! Help me clean it up!

First, start by moving the 1 from index 3 to index 0. Try to do this in a single step by using both .pop() and .insert(). It's OK if it takes you more than one step, though!

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hey, I think the challenge interpreter is going kind of insane. I had the same error message. I solved it by reloading the page. And be careful. You donΒ΄t need the str() method in the first remove statement. And don't forget to remove the list.

Jay Reyes
seal-mask
.a{fill-rule:evenodd;}techdegree
Jay Reyes
Python Web Development Techdegree Student 15,937 Points

I was looking for this. I was trying to use messy_list.copy() instead of messy_list[:] BUT we haven't learned about slices yet ;)

John Smith
seal-mask
.a{fill-rule:evenodd;}techdegree
John Smith
Python Web Development Techdegree Student 1,052 Points

That's odd, for some reason I still cannot get it to work even after refreshing the page multiple times. Does anyone have any idea why this doesn't work? Maybe the interpreter is looking for a specific way of coding this problem.