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

tyler bucasas
tyler bucasas
2,453 Points

slices.py

im just stuck on step 2 of this challenge. specifically the part when it asks for the last 4 items.

slices.py
def first_4(list):
    return(list[:4])

def first_and_last_4(list):
    return(list[:4 and -4::-1])

1 Answer

Steven Parker
Steven Parker
231,269 Points

Here's a few hints:

  • you can't combine values with logic operators to make one slice do two things
  • but you can concatenate separate strings together with the "+" operator
  • you might use two different slices for the first and last parts
tyler bucasas
tyler bucasas
2,453 Points
def first_4(list):
    return(list[:4])

def first_and_last_4(list):
    return(list[:4]) + (list[-4::-1])

Steven Parker am I on the right track?

Steven Parker
Steven Parker
231,269 Points

Very close! But you don't need to reverse the order of the last 4 items.

Also, you don't need to enclose the terms in parentheses (but it doesn't cause a problem in the challenge).