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 Collections (2016, retired 2019) Slices Slice Functions

Brent Capuano
Brent Capuano
949 Points

Why are these quizzes so hard? Help

Im not sure how to do this. I get so discouraged that I cant move forward and there are no hints. Can someone show me how to do this? please and thank you. I wish the quizzes didnt span all different lessons. The quizzes are so frustrating

slices.py
list = [1,2,3,4,5,6]

def first_4(x):
    x = [0:5]
    return(x)

2 Answers

Steven Parker
Steven Parker
230,231 Points

Here's a few hints:

  • you only need to define the functions, you won't need to supply any data
  • there should not be an equal sign between the slice brackets and the item being sliced
  • you won't need to assign a variable if you create the slice as part of the "return" statement
Leo Marco Corpuz
Leo Marco Corpuz
18,975 Points

I'm past this challenge already, but I forgot how the third part of this challenge was done. It's asking for the odd index numbers. First, I created an empty list then I for-looped through the range of the length of the list argument . Then I set an if condition where only indexes not divisible by 2(i % 2 != 0) would be added to the empty list. I returned that list and it was wrong.

For the first part of this challenge. I passed when returning [0:4] instead of [0:3]. Isn't [0:3] the first four items?

Steven Parker
Steven Parker
230,231 Points

The odd indexes can be obtained using a slice with a start value of 1 and a step of 2 ([1::2]). All of the tasks in this challenge can be completed without using loops.

When using a stop value, the number is for the index to stop before. So [0:4] (or just [:4]) would return the indexes 0,1,2,3 (the first four).

Brent Capuano
Brent Capuano
949 Points

i still havent gotten this correct. can you show me the answer?

Steven Parker
Steven Parker
230,231 Points

Brent – Try applying the hints given here, and if you still have trouble, create a fresh new question where you can show your code as it is now and we can take it from there.

Leo Marco Corpuz
Leo Marco Corpuz
18,975 Points

Ok thanks! I forgot about that.

Steven Parker
Steven Parker
230,231 Points

Leo Marco Corpuz — Glad to help. You can mark the question solved by choosing a "best answer".
And happy coding!