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 Specific Index value

How do I finish this problem ? I don't understand what index -2 means

I got var_one correctly with an index of 5 but when negatives are involved I don't understand how to do it.

index.py
var_one = [ 1, 2, 3, 4, 5, "t"]
var_two = [ -5, -4, -3, -2, "e"]

1 Answer

Ben Reynolds
Ben Reynolds
35,170 Points

A negative index value starts at the end of the collection and counts backward, so an index of -2 would be the second to last item.

The confusing part is that an index normally starts counting at 0. But with a negative value, let's say -n, you're targeting the item at the "nth" position from the end.

Greg Kaleka
Greg Kaleka
39,021 Points

Think of it this way - the index still starts at zero, which is the first element. Then move to the left one to get to -1. No more elements to the left? You have to wrap around to the end, then.