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

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
210,066 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]