Python Python Collections Slices Slice Functions

Aaron Fry
Aaron Fry
814 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

caits
caits
2,755 Points

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
814 Points

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