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

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!