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

Tomas Skacel
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Tomas Skacel
Python Development Techdegree Graduate 8,994 Points

More on seq[::-1].

seq = ['a','b','c','d','e']
seq[::-1]
seq[4:0:-1]

The second line of the code reverses the list. By leaving the start and stop values empty it starts from the end (seq[4]) and finishes at the start (seq[0]).

If we wanted to write this without leaving out the start and stop values, how would we go about it? The third line almost accomplishes this, but it leaves the 0th entry of seq out because the stop value is not included. If my understanding is correct, then having the stop value as -1 would be equivalent to having the stop value as 4, so that wouldn't work. So what's the solution here? Is there even one?

Thank you!

1 Answer

Steven Parker
Steven Parker
229,783 Points

The ending position is after the item number provided, so as you noticed, 0 does not include the first value. The argument to point before the first value is "None", so you would code your slice like this:

seq[4:None:-1]