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 Introducing Lists Meet Lists Indexing

if lists can have only 0 and 1 how do we get [-2]

lists

2 Answers

If your list is two elements you could use [-2] to get the first element because negative index start with[-1] not [0].

but if your list is just one element you still can use[-1] not [-2] because it will be out of range.

Good luck :)

Samuel Morgan
Samuel Morgan
2,226 Points

Using [-2] would be out of range if your list only has one element. If you had two elements, [-2] would actually list your first element!. It's an easy way to list elements at the end of the list without having to count how many elements there are.