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

Brandon White
seal-mask
MOD
.a{fill-rule:evenodd;}techdegree seal-36
Brandon White
Treehouse Moderator

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!