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

George Clement
George Clement
2,816 Points

not sure why this is not being accepted. am I returning the wrong format?

when I define and use this method in idle I get

first_and_last_4("ttttffffoooo") 'ttttoooo'

len(first_and_last_4("ttttffffoooo")) 8

slices.py
def first_4(iterable_obj):
    the_four_list = []

    for _ in iterable_obj:
        if len(the_four_list) < 4:
            the_four_list.append(_)

    return the_four_list


def first_and_last_4 (iterable_obj):
    return ("{}{}".format(iterable_obj[0:4], iterable_obj[-4:]))

1 Answer

Alex Koumparos
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,887 Points

Hi George,

You're nearly there. You've correctly got the first and last four items, but you're returning them as a string when the challenge wants you to keep the type as a list.

There are various ways that you can combine two lists, but perhaps the easiest way is to simply 'add' them, using the + operator. For example:

 ['a', 'b', 'c'] + ['d', 'e', 'f']  # ['a', 'b', 'c', 'd', 'e', 'f']

Hope that points you in the right direction.

Cheers

Alex