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

Chad Kusuno
Chad Kusuno
6,789 Points

1st Challenge? student_gpas = [4.0, 2.3, 3.5, 3.7, 3.9, 2.8, 1.5, 4.0] sliced_gpas = student_gpas[2:5:2]

Challenge Task 1 of 1

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.

I think this is right. When I run it in the editor is gives me the correct answer. [3.5, 3.9]. The preview is not working and it doesn't like my answer.

Please confirm it's correct or that I'm not getting it.

Thanks, -Chad :)

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

sliced_gpas = student_gpas[1:4:2]

2 Answers

Hi Chad,

The number after the second colon is a step counter. [2:5:2] 👈🏾 This should return the third item up to (but not including) the sixth item in the list. The second 2 tells the interpreter to skip every other item though. So you are correct in that it would return 3.5 and 3.9, but that is not the correct answer because 3.7 is not included.

Remove the second colon and the second 2 and you should be good to go.

Chad Kusuno
Chad Kusuno
6,789 Points

I was reading the question incorrectly. :)

Chad Kusuno
Chad Kusuno
6,789 Points

Thank you Brandon. You rock!