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 trialIdan shami
13,251 PointsSlice challenge, can I get an explanation?
Hi, I don't know what to do in this challenge, please give me a simple explanation and an example of what should be in the end.... though don't give me the answer, I want to try alone.... but I can't understand what I need to do...
thanks, Idan. (:
def first_4(iterable):
return iterable[:4]
def first_and_last_4(iterable2):
return iterable2[:4] + iterable2[-4:]
def odds(item):
1 Answer
Oszkár Fehér
Treehouse Project ReviewerHi Idan
Slicing it takes a 3 argument ["1st":"2nd":"3rd"] ---> [::] The 3rd arg it defines the step of the slice Example:
list = [0, 1, 2, 3, 4, 5, 6]
list[::1] ---> it takes the list elements one by one [0, 1, 2, 3, 4, 5, 6]
list[::2] ---> it takes every second element from the list : output ---> [0, 2, 4, 6]
list[::3] ---> [2, 5]
with negative it takes the list backwards: list[::-1] ----> [6, 5, 4, 3, 2, 1, 0]
so the 3rd arg decide the step of the elements and with minus sign the reversed items from the list decided by number of steps
list[::-3] ----> [4, 1]
I hope this helps Let me know if indeed helped you to understand
Idan shami
13,251 PointsIdan shami
13,251 PointsI completed the challenge!!!!!!! (: but I have a question, for solving the challenge I used this code:
return item[1:2544:2]
If the iterable is higher than 2544(indexes), it will show the odd indexes, but not till the end... Is there a way to set the to that he will be until the end of the iterable? any iterable... thanks ! and I hope you understand what I'm saying XD because my English isn't so good... have a fantastic day!
Idan.