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 (Retired) Lists Redux Manipulating Lists

Saloni Gandhi
Saloni Gandhi
1,531 Points

lists.py

please help me out

the_list = ["a", 2, 3, 1, False, [1, 2, 3]]
new=the_list.pop(3)
the_list.insert(0,new)
# Your code goes below here


del the_list[1]
del the_list[4]
del the_list[5]
Michel van Essen
Michel van Essen
8,077 Points

I just noticed that you're first task is not what the challenge asked for. The first task asked to pop and insert with one line of code, so I would replace your first line of code with: the_list.insert(0, the_list.pop(3))

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

Good job, Michel, for solving it in a single line.

This is one case where the challenge grader is lenient. The specific text says "You can do this in one step..." [emphasis mine] but it is not required.

Michel van Essen
Michel van Essen
8,077 Points

Got you, that makes sense! Thanks for your presence by the way, you are on a lot of topics that helped me out huge!

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

One hazard when removing items from a list is the indexes change after each deletion. While your three del statements would work if the index were constant, they need to be adjusted to account for the changing indexes:

the_list = ["a", 2, 3, 1, False, [1, 2, 3]]
new=the_list.pop(3)
the_list.insert(0,new)
# Your code goes below here


del the_list[1]  # <-- This is OK
del the_list[3] # <-- False shifted to index 3 when 'a' is removed
del the_list[3] # <-- List shifted to index 3 when False is removed

One way to avoid the changing indexes is to remove from the back towards the front:

del the_list[5] 
del the_list[4] 
del the_list[1]