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

Help me!!

Great work! OK, this second one should be pretty similar to the first. Make a new function named first_and_last_4. It'll accept a single iterable but, this time, it'll return the first four and last four items as a single value. Please write the code.

slices.py
def first_4(a):
    return a[:4]
def first_and_last_4(b):
    return b[-1:-5:-1] + b[:4]

2 Answers

Christopher Shaw
seal-mask
PLUS
.a{fill-rule:evenodd;}techdegree seal-36
Christopher Shaw
Python Web Development Techdegree Graduate 58,248 Points

You are returning the last 4 first, and in reverse order. Your should put the first 4 first, then the last 4.

def first_and_last_4(b):
    return b[:4] + b[-4:]

Thanks a lot! ;)

Hi there!

You're really close!

At the moment you're reversing the string you get back from the last four part by starting at the last character (-1) stopping at the end -4 (-5) and stepping by minus 1. so

"0123456789"[-1:-5:-1]
>>>"9876"

You want to find a way to start at the 6 (four steps from the end) and step forwards to the end :)

Hope it helps

Thank you sooo much! ;)