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

helmi al kindy
helmi al kindy
1,371 Points

This wasn't covered in the videos

I have tried using boolean operators but it won't go through. What can I do ?

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

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

def first_and_last_4(e):
  return e[0:4] and e[-1:-4]

2 Answers

Tony Gibbons
Tony Gibbons
1,573 Points

This worked for me:

def first_and_last_4(x):
    return x[:4] + x[-4:]
Hanley Chan
Hanley Chan
27,771 Points

Hi,

This function should return an iterable made of the first 4 and the last 4 items of the iterable. There is no need to use boolean operators here. You could concatenate the first four items and the last four items (iterable1 + iterable2).

Looks like you got the first four items correct in your code, but the last four needs work.

Hint: You can get the number of elements in the iterable e with the len() function. You may want to use this in your slice to get the last four elements.

def first_and_last_4(e):
  return e[0:4] and e[-1:-4]

Hope this helps.