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 trialjames john
Courses Plus Student 193 PointsMake a function name first_and_last_4 that accepts an iterable and returns the first 4 and last 4 items
why doesn't this function give me the first and the last 4 ?
def first_and_last_4(item):
list_1 = item[0:4] + item[-1:-5:-1]
list_2 = list_1.sort()
return list_2
3 Answers
Nicholas Ah Kun
2,419 Pointsalso for list_1 remember that you don't need a start position if you're getting the first 4.
And to go 'backwards' through a list just use a -, you don't need a negative stepping. I.e. item[-4:] tells python to go 4 places from the back of the list, to the end of the list.
Dan Johnson
40,533 PointsThe sort method doesn't return a value, it sorts the calling list in place. You won't have to use list_2 to save the result.
Giovanni Cortes
iOS Development Techdegree Student 4,803 PointsIs easy if you use the +
and len()
operators with the list.
You can create two list with the same list, the list1 that you are using is correct. The second one is:
- You need to start with the last 4 digits of the list, so, how I obtain it? You need to know the length of the list and subtract 4 elements.
- Continue until the end of the list
- Add the first list and the second list