Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Sequences Sequence Operations Creating Slices

How do I know what elements I need?

I need to make a slice of the 3rd through 5th elements in the list. But does that mean 3rd starting at 1 like a human, or starting at 0 like Python? I tried slicing [3:6] and got an 'incorrect values' answer, so I tried [4:6] and [4:7] and got the same result.

slices.py
student_gpas = [4.0, 2.3, 3.5, 3.7, 3.9, 2.8, 1.5, 4.0]
sliced_gpas = student_gpas[4:6] 

1 Answer

Steven Parker
Steven Parker
210,131 Points

You're right about "starting at 0 like Python", so that means the third element would have an index of  2 . And since slices are exclusive of the stop value, and the final index should be 4, then the stop value is  5 .

sliced_gpas = student_gpas[2:5] 

I was adding 1 to the wrong number, oops. Thanks for breaking it down. Wish there was a better way to explain that we want the third item in the list according to humans, second according to Python.

Steven Parker
Steven Parker
210,131 Points

It's still the 3rd item to Python, it just starts counting with 0 for the first.