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 trialJohn Smith
Python Web Development Techdegree Student 1,052 PointsWhat's the simplest way to solve this using insert and pop method?
Please help!
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)
2 Answers
Grigorij Schleifer
10,365 PointsHi Ruddy, I think you use the simplest way to solve this challenge already . 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
Python Web Development Techdegree Student 1,052 PointsIt 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
10,365 PointsHey, 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.
John Smith
Python Web Development Techdegree Student 1,052 PointsThank 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.
Jay Reyes
Python Web Development Techdegree Student 15,937 PointsI 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
Python Web Development Techdegree Student 1,052 PointsThat'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.
John Smith
Python Web Development Techdegree Student 1,052 PointsThis 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!
John Smith
Python Web Development Techdegree Student 1,052 PointsJohn Smith
Python Web Development Techdegree Student 1,052 PointsIt 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)