Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Collections (2016, retired 2019) Slices Slice Functions

Aaron Fry
Aaron Fry
826 Points

What is the output it wants?

I can make it return an integer or a string but it doesn't like either one so what does the project want back? Does it want two separate single values back for the start and ending? I have been working on this code for a while and I can make it do what I want but I don't understand the question. Someone please clarify the question better.

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

def first_and_last_4(item):
  mixed_return = ''
  if len(item) < 8:
    for number in item:
      mixed_return += str(number)
  else:
    mixed_return = ''
    frst = item[0:4]
    lst = item[-4:]
    for number in frst:
      mixed_return += str(number)
    for number in lst:
      mixed_return += str(number)
  return mixed_return

1 Answer

Are you on task 2? If so, it seems like you're overthinking this. You need to return two slices combined. Example: for a list of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], you would want to return [1, 2, 3, 4, 7, 8, 9, 10]. It can be done in a single statement. I hope that helps.

Aaron Fry
Aaron Fry
826 Points

I was wondering if they just wanted a single list of the first 4 and last. Thank your response!