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 Basics (2015) Python Data Types List cleanup

why is it not working

can you help fast

lists.py
messy = [5, 2, 8, 1, 3]

del messy[-3]
del messy[-4]

2 Answers

Jake Goodman
Jake Goodman
3,381 Points

When you delete the -3 index of messy, your list would now look like this:

[5, 2, 1, 3]

Now, the number 2 is no longer at the -4th index of this list because the list has changed. The number 2 is now at the -3rd index, or the 1st index.

Jeff Wilton
Jeff Wilton
16,646 Points

You're super close. The only thing you have to keep in mind is that after your first delete statement, there is one less item in the array.

original array:
messy = [5, 2, 8, 1, 3]

array after del messy[-3]:
messy = [5, 2, 1, 3]

How do we delete the "2" now? It's still the third from the right, so it would still be del messy[-3]

Full example:

messy = [5, 2, 8, 1, 3]

del messy[-3]
del messy[-3]