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

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)

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

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?

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!

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.

Thank you Grigorij, I found the answer on this link.

https://teamtreehouse.com/community/syntaxerror-for-letter-in-messylist

It was much more simple than what it seems.

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

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.

This post helped out, I finally found the answer. It was a lot more straight forward than I thought.

https://teamtreehouse.com/community/syntaxerror-for-letter-in-messylist

Thanks for helping Grigorij!