Python Python Collections (2016, retired 2019) Lists Removing items from a list

Ruddy Jimenez
Ruddy Jimenez
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)
Ruddy Jimenez
Ruddy Jimenez
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,352 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?

Ruddy Jimenez
Ruddy Jimenez
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,352 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
Jay Reyes
Python Web Development Techdegree Student 15,935 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 ;)

Ruddy Jimenez
Ruddy Jimenez
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.

Ruddy Jimenez
Ruddy Jimenez
Python Web Development Techdegree Student 1,052 Points

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!