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

Slicing With A Step

Having trouble with an example in the video 'Slicing With A Step.'

When I do: my_list = list(range(20))

my_list[7-1] I get 6 not [7, 6, 5, 4]

It should be: my_list[7-1]

[7, 6, 5, 4, 3, 2]

1 Answer

The start of the slice and the step should be separated by colons, 2 since you don't have an end to the slice. Without them, you're asking for the index of 7-1 which is 6.

my_list[7::-1]

#or more generally
some_list[start:end:step]

Thank you Seth for the explanation.