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

Lindsay Grizzard
Lindsay Grizzard
6,403 Points

Make a function name first_and_last_4 that accepts an iterable and returns the first 4 and last 4 items in the iterable.

I can't quite figure out why this wont run. I have my index going for 0-3 (or 1-4) and my negative at -4.

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

def odds(idx):
  idx = "Oklahoma" 
  return idx[1::2]

def first_and_last_4(idx):
  idx(range(0, 50)
  print idx[:3] + idx[-4:]

1 Answer

Devin Scheu
Devin Scheu
66,191 Points

Your code should look something like this:

def first_4(iterable):
  return iterable[0:4]

first_4([66, 333, 222, 1, 1234])


def first_4(iterable):
  return iterable[0:4]

first_4([66, 333, 222, 1, 1234])

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

def first_and_last_4(iterable):
  return first_4(iterable) + iterable[len(iterable)-4:]