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

python slicing

I am having a really tough time solving this challenge. It wants me to get the first and last 4 items of a list sliced out, but not have them be separate lists. The little hint when I get it wrong says it wants to get back [1, 2, 3, 4, 46, 47, 48, 49]. How do I get that back?

slices.py
def first_4(list):
  return list[:4]
def odds(list):
  return list[1::2]
def first_and_last_4(list):
  return list[:4], return list[46:]

nevermind, guys. I figured it out. Apparently there is this handy thing called "+" ... :-P

1 Answer

Sean T. Unwin
Sean T. Unwin
28,690 Points

We are unable to return something more than once from a function, therefore we may only have 1 return statement.

Since we have 2 steps in this function: 1) Get the first 4 items in a list and 2) Get the last 4 items in a list.

One way to do this would be to declare a variable as an iterable for step 1. Following that we could use extend to include the last 4 items for step 2. Finally, when we return that we should have our first 4 and last 4.

... or you could simply use a + to join the 2 then return that. ;-)