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

Clayton McDaniels
Clayton McDaniels
5,788 Points

What am I doing wrong? I don't know how to make it return a single(combined) value.

What am I doing wrong? I don't know how to make it return a single(combined) value.

slices.py
def first_4(x):
    for y in x:
        return x[0:4]
def first_and_last_4(a):
    for b in a:
        return a[0:4] and a[-1:-4:-1]

1 Answer

When using return, it returns from the very time it is called, so a loop is not necessary as it will return and end the execution of the code, on the very first iteration of the loop

use + instead of and

def first_4(x):
     return x[0:4]

def first_and_last_4(a):
    return a[0:4] + a[-4:]
Clayton McDaniels
Clayton McDaniels
5,788 Points

Thank you. I realized that after the post. Sometimes my brain is a little slow. But I greatly appreciate it.