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 Sequences Sequence Operations Creating Slices

pthon sequence challenge

Question :

Create a slice of the provided student_gpas list that includes the 3rd through the 5th elements in the list. Assign the slice to a variable called sliced_gpas.

student_gpas = [4.0, 2.3, 3.5, 3.7, 3.9, 2.8, 1.5, 4.0] sliced_gpas=student_gpas[3:6]

I am getting the below error

Bummer: Whoops, looks like your slice contains incorrect values. Remember that slices are exclusive of the stop value.

Please let me know what was wrong in this code ?

slices.py
student_gpas = [4.0, 2.3, 3.5, 3.7, 3.9, 2.8, 1.5, 4.0]

sliced_gpas = student_gpas[3:6]

2 Answers

sliced_gpas = student_gpas[2:5]
#remeber that python sequences start from 0 so when it asked for the third element it's 2. 
#ya its kinda weird but you get used to it
Steven Parker
Steven Parker
231,272 Points

It looks like you remembered about slice ranges being "exclusive of the stop value", but forgot that indexes in Python begin at 0 instead of 1.

So to slice the "3rd through the 5th elements", the index values would be 2 and 5:

sliced_gpas = student_gpas[2:5]