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

Emilio Andere
Emilio Andere
495 Points

Why is this incorrect

check please

lists.py
messy_list = ["a", 2, 3, 1, False, [1, 2, 3]]
messy_list.pop(3)
messy_list.insert(0, 1)
messy_list.pop(1)
messy_list.remove([1, 2, 3])
messy_list.pop(4)
# Your code goes below here

1 Answer

Teal Larsen
Teal Larsen
21,066 Points

When you begin your list contains 6 things that have the indices 0-5. You then remove 1 and insert 1, so you still have 6 things. Then you remove 2 items (using the pop and remove methods) which leaves 4 items in your list, they have the indices 0, 1, 2, and 3. So when you call the messy_list.pop(4) you are trying to remove the item at index 4 but you don't have an index 4. Because the index starts at 0, your 4th and final item in the current list has an index of 3. To remove the last item you should use messy_list.pop(3) or just messy_list.pop as that will remove the last item by default.

Emilio Andere
Emilio Andere
495 Points

Teal you're very helpful, THANK YOUU