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

qifeng sun
qifeng sun
6,274 Points

SyntaxError: for letter in messy_list

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!

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

# Your code goes below here

for letter in messy_list
    if letter != 3:
        messy_list.pop
    else letter.insert(0)

Make sure that when you are using .pop() you are supplying the index of the list. ie messy_list.pop(4) would remove the 'False' value from the 4 index position.

Also, when you are using .insert() you'll need to supply the index, and the thing you want to insert to the list.
ie messy_list.insert(0, "Thing") would insert the string "Thing" into the beginning of the list at 0 index position.

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,457 Points

The challenge is more straightforward than you're making it.

You're given the index of the item to pop (the 1 is at index 3) so no need to loop through to look for it.

item = messy_list.pop(3)

Then insert item back into list

messy_list.insert(item)

These two statements can be chained together into one. Can you figure out it can be done?

Post back if you need more help. Good luck!!!

Aananya Vyas
Aananya Vyas
20,157 Points
messy_list.insert(0,messy_list.pop(3))

Thanks!