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 (2016, retired 2019) Slices Slice Functions

Benjamin Harris
Benjamin Harris
8,872 Points

Return first four and last four, items from a list, as a single value?

will add up the total value of the required items.

def first_4(your_list): return sum(your_list[:4] + your_list[-4:])

will return the required items in a single list

def first_4(your_list): return your_list[:4] + your_list[-4:]

I must be misunderstanding the question?

slices.py
def first_4(your_list):
    return sum(your_list[:4] + your_list[-4:])

2 Answers

Kyle Knapp
Kyle Knapp
21,526 Points

The question isn't asking for the sum of the first and last 4 values in the list, it's asking for a new list containing the first and last 4 items from the original list.

sum() isn't required to concatenate lists. You can simply return slice1 + slice2.

Also, as Alexander mentioned, the function needs renamed.

  • The function should be called first_and_last_4, not first_4 (Remember: the other function you defined was called that!)
  • Usage of the sum function is unnecessary and in fact with cause an error

Other than those two problems, everything looks fine. :+1: