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 trialAngela Anders
1,844 Pointsfirst_and_last_4
I'm not sure how to add two iterations together, so I took a stab at it but I don't know what I'm doing incorrectly. Hopefully I'm on the right track, but I have no idea.
def first_4(iterable):
first_4 = iterable[0:4]
return first_4
def first_and_last_4(iterable):
first_and_last_4 = iterable[0:4] + iterable[-1:-5:-1]
3 Answers
Chris Freeman
Treehouse Moderator 68,441 PointsYou are headed in the right direction. When working with slices, the second perimeter stop must be after the first parameter start. In your code above, the -1 comes after the -5. This results in an empty list that is then reversed with the -1 step parameter, which is still an empty list.
If you want to go from any point to the end of the list, leave the stop parameter empty. Using this idea, what would be a good starting point to get the last four items?
Yes, using the + to add two iterables is valid. If both are strings than a string is returned if both are lists a new list is returned.
Post back if you need more help. Good luck!!!
Angela Anders
1,844 PointsWeird....I just copied and pasted and it worked now. Literally two seconds ago it didn't.
Problem solved. :)
Chris Freeman
Treehouse Moderator 68,441 PointsGreat! Way to keep at it. Bonus comment:
You can take a slice of a slice by appending another slice notation:
>>> [1,2,3,4,5,6,7,8,9,10,11,12]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
>>> [1,2,3,4,5,6,7,8,9,10,11,12][1::2]
[2, 4, 6, 8, 10, 12]
>>> [1,2,3,4,5,6,7,8,9,10,11,12][1::2][::3]
[2, 8]
Angela Anders
1,844 PointsOh cool! I love this stuff! Growth mindset! :) Thanks again!
Angela Anders
1,844 PointsAngela Anders
1,844 PointsI figured it out last night. Thanks!
Angela Anders
1,844 PointsAngela Anders
1,844 PointsChris, Now I have new issue with reverse_evens on the 4th challenge. To figure this out I went into interactive mode in the shell:
Angela Anders
1,844 PointsAngela Anders
1,844 PointsI then used this for my function; however, it's not working and I don't know why.