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

Charles Harpke
Charles Harpke
33,986 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.

Is there a way to contain the first and last 4 values after the utterable?

here is my code:

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(iterable):
  return(iterable)[0:4]

15 Answers

Anthony Liu
Anthony Liu
11,374 Points

I am not completely sure if the way I did it is the most efficient. In the "first_and_last" function

1.) I created a list for the first 4

2.) a list for the last 4.

3.)Combined the 2 lists

4.) returned the new list

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

That's pretty much the only way to do it. You can avoid creating some variables, though, by just combining the slices directly.

Herb Bresnan
Herb Bresnan
10,658 Points
def first_4_last_4(item):
    return item[:4] + item[-4:len(item):1]

item[:4] gives the first four numbers by index [0, 1, 2, 3] concatenated with the START is -4: STOP is len(item):STEP is 1 So start at the -4 index position, stop at the end of the list, and step by 1 until you reach the end of the list.

Anthony Attard
Anthony Attard
43,915 Points

Could be simplified to:

def first_4_last_4(item):
    return item[:4] + item[-4::1]
Martin Krkoska
Martin Krkoska
10,514 Points
def first_and_last_4(iterable):
    return iterable[:4] + iterable[-4:]

this is working too and you dont need len(item) or step

Matt Milburn
Matt Milburn
20,786 Points

Hey guys, just wanted to offer up the correct answer here. You're welcome!

def first_and_last_4(iterable):
    return iterable[:4] + iterable[-4:]
Anthony Jackson
Anthony Jackson
19,505 Points

If you want to do it in one line you can try

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

this will reuse the code you already create in the first_4() function and combine it with your code for getting the last four

Hey Anthony, I was doing some review and I caught your answer here. I wouldn't have thought to do that. It's a nice little mind expander for me. I get so focused and boxed in sometimes I lose sight of the possibilities. Thanks

i know its been 4 months since you last post the question but i just got past through now. i hope it will help others

wafic fahme
wafic fahme
3,309 Points
od = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
return od[0:4] + od[-4:len(od)]
Kishore Kumar
Kishore Kumar
5,451 Points

def first_and_last_4(arg2): list = arg2[:4] + arg2[len(arg2)-4:len(arg2)] return list

Jarrod Gillis
Jarrod Gillis
16,760 Points

def first_and_last_4(timberwolves): return timberwolves[:4] + timberwolves[-4:]

def first_and_last_4(iterable ): ite1 =iterable[0:4] ite2 = iterable[-4:] result = [] result.extend(ite1) result.extend(ite2) return result

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

def first_and_last_4(something): return something[:4] + something[-4:]

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

def first_and_last_4(text):
    first  = ''.join(text[:4])
    second = ''.join(text[-4:])
    return '{}{}'.format(first,second)

this should work also but it doesnt. Can anyone tell me why?

Jan Durcak
Jan Durcak
12,662 Points

this one ? def first_and_last_4(a): c= a[0:4] d = a[-1:-5:-1] a = c+d return a

Jan Durcak
Jan Durcak
12,662 Points

'''' def first_4_last_4(a): c= a[0:4] d = a[-4:] a = c+d return a ''''

victor chingtham
victor chingtham
1,548 Points

This is the correct one, already completed the first_4 in the task one

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

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

Sean M
Sean M
7,344 Points

here's what I did:

#practice
messy_list = [1,2,3,4,5,6,7,8]

def first_4(a_list):
    return a_list[:4]

def last_4(a_list):
    return a_list[-4:]

def first_and_last_4(a_list):
    return first_4(a_list) + last_4(a_list)


print(first_and_last_4(messy_list))
return iterable[0:4]+ iterable[45:50]
Kenneth Love
Kenneth Love
Treehouse Guest Teacher

This is not the right way to do it. This won't work if you don't know the length of the iterable. You need to come up with a solution that'll work on ANY iterable.