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 (Retired) Slices Slice Functions

Dhruv Ghulati
Dhruv Ghulati
1,582 Points

Unsure about how to return multiple things (first 4 and last 4) in a function

Unsure of how to do this. What have I done wrong with my code?

last_4 I have put as [len(i):len(i)-4:-1] - backwards counting, starting from the highest instead of lowest (as shown in video).

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

def odds(i):
  return i[1::2]

def first_and_last_4(i):
  list=list[]
  first_4=i[:4]
  last_4=i[len(i):(len(i)-4):-1]
  list.append(first_4)
  list.append(last_4)
  return list()

2 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

list=list[] doesn't make any sense. I'm surprised it's not throwing a syntax error for you (or maybe it is).

You know how to get the first four items and you should know how to get the last four items. Slices work like lists, so they can be added together. Return the concatenation of the two slices.

Stephen Link
Stephen Link
3,685 Points

Kenneth is correct about list = list[]. I tried that in the interpreter and immediately received an error. Rename that list as something other than a Python keyword. Also, go back and review slices and then take another look at how you're trying to get the last 4 items. Finally, are you submitting all 3 functions to the grader? If so that is going to be another source of trouble for you. You have a function named first_4 and then your first_and_last_4 function has a variable named first_4.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

The first_4 variable inside the function actually won't matter. Python's scope rules keep those from conflicting.