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

Oprea Mihai
Oprea Mihai
5,592 Points

first_and_last

Why it doesn't work here because I try on other console and it works ?

slices.py
def first_4(list_1):
    try:
        list_1=list_1[0:4]
    except:
        list_1=list_1.list()
        list_1=list_1[0:4]
    return list_1
def first_and_last_4(list_2):
        try:
        first_4=list_2[:4]
        last_4=list_2[:-5:-1]
        list_2=first_4+last_4
        list_2=''.join(map(str,list_2))
    except:
        list_2=list_2()
        first_4=list_2[:4]
        last_4=list_2[:-5:-1]
        list_2=first_4+last_4
        list_2 = ''.join(map(str, list_2))
    return list_2

1 Answer

Steven Parker
Steven Parker
229,744 Points

Here's a few hints:

  • you wont need "try" and "except" for this challenge, but if you did, they should both be indented the same
  • you don't need to reverse the order of the "last 4" slice
  • the "last 4" slice should begin 4 items from the end (and go to the end)
  • you won't need "join" since you'll be returning a list and not a string