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

Martin Heinvee
Martin Heinvee
872 Points

Task about using slices-solution not passing the challenge.

The last end of the returned answer was indeed backwards, thanks Steven. But still the solution is not passed. Any ideas?

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

def first_and_last_4(item2):
    uus=[]
    for i in item2:
        character=str(i)
        uus.append(character)
    string1 = ''.join(uus) 
    return string1[0:4:1]+string1[-4::1]

2 Answers

Steven Parker
Steven Parker
231,007 Points

I don't quite understand what you're intending to do with the list, loop, and join. Why not apply the slices directly to the argument like you did for the first function?

And in future, you can always edit or add a comment to an existing question, you don't need to start another one for the same issue.

Martin Heinvee
Martin Heinvee
872 Points

Seems that I overthought it and made it much more complicated than it really was. Initially I understood that in addition to a string input we could also have a list as an input, since it is also iterable. So, for the list input I separated the first and last four and joined them together in order to return a SINGLE VALUE. I thought that in case of input [1,2,3,4,5,6,7,8,9] answer must be single value '12346789'.

NOW I GOT IT and i managed to pass this challenge, thanks for the comments, these led me to right solution. Thanks!

The right solution was easy:

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