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

Nursultan Bolatbayev
Nursultan Bolatbayev
16,774 Points

slicing range choice

Hello. In this third function to choose first 4 and the last 4 elements in the list I used that way. But Im not sure maybe there are some tools in slicing to make it in one line?

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

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

def first_and_last_4(arg): first_list = arg[:4] second_list = arg[-4:] final_list = first_list + second_list return final_list

slices.py
def first_4(arg):
  return arg[:4]
def odds(arg):
  return arg[1::2]

def first_and_last_4(arg):
  first_list = arg[:4]
  second_list = arg[-4:]
  final_list = first_list + second_list
  return final_list

1 Answer

Kourosh Raeen
Kourosh Raeen
23,733 Points

Hi Nursultan - How about this:

def first_and_last_4(arg):
  return arg[:4] + arg[-4:]
Nursultan Bolatbayev
Nursultan Bolatbayev
16,774 Points

Hi, Kourosh. That was obvious)) I didnt see What a stupid question from me. Thanks, anyway. Yeah, that works)