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

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!