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

Greg Maddox
Greg Maddox
1,471 Points

i can't seem to understand this problem. i get the right answer when i do it in the work station. is it .append()

When i .join() the 2 in the workspace i get the right string, but in this module it says sequence error, looking for str and got list.

slices.py
def first_4(iterable):
    first_four = iterable [:4]
    return first_four

def first_and_last_4(iterable):
    first_last_4 = ''
    stuff = ''
    first_last_4.append(iterable [:4])
    first_last_4.append(iterable [-4:])
    return stuff.join(first_last_4)
Martin Heinvee
Martin Heinvee
872 Points

Hi, I have the same problem, getting the right answers but no pass. I posted my solution also.

Greg Maddox
Greg Maddox
1,471 Points

i've tried several variations in the python shell, and they all work. They still won't work in the module though. In fact, in the next video he ends with a solution to the problem, i went back and that doesn't even work.

first_last_4 [:] = [''.join(first_last_4[:])]

not sure what the issue is.

Martin Heinvee
Martin Heinvee
872 Points

I tried your code and at first it gave an error if I used 'jupiter&mars' as input. I modified it and got it working for strings. However I understand the challenge so that it also has to take list as input for example [1,2,3,4,5,6,7,8,9]. I understand that for that we should have '12346789'. Anyway, in case of string input:

def first_4(iterable): first_four = iterable [:4] return first_four

def first_and_last_4(iterable): first_last_4 = [] stuff = [] first_last_4.append(iterable[:4]) stuff.append(iterable[-4:]) return (''.join(first_last_4))+(''.join(stuff))

1 Answer

Martin Heinvee
Martin Heinvee
872 Points

Hi, I got the correct answer and passed. It turned out that the only input can be string not the list also as I understood. My working solution is the following:

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