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

Im stuck on the slices challenge :(

Hello,

I am stuck on task 2, where it asks for the first and last 4 items in a given iterable, returned as a single value. I have attached my code, as it keeps saying the function does not return the right answer. Any suggestions would be much appreciated :(

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

def first_and_last_4(numbers2):
    numbers3 = numbers2[0:4] + numbers2[-1:-5:-1]
    numbers3 = ''.join(str(element) for element in numbers3)
    return numbers3

1 Answer

I have since solved it, I was overthinking. The challenge wanted the last four items in the same order, the piece of code below reverses them: numbers3 = numbers2[0:4] + numbers2[-1:-5:-1]

Solution: numbers3 = numbers2[0:4] + numbers2[-4::]

Thanks anyway.