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

Python Collection Challenge task 3 Wow what is the solution to this task? I cannot solve it for the life of me

How exactly do you make the iterable return both the first 4 letter and last 4 letters?

slices.py
def first_4(x):
  for i in x:
    return x[:4]

def odds(y):
  for i in y:
    return y[1::2]

def first_and_last_4 (z):
  for i in z:
    return z[:4] and z[-4:]

1 Answer

Hi Halcyon,

You don't need a for loop in any of the tasks. You only need to return a slice of the iterable that was passed in. Your for loop isn't really doing anything because on the first loop you're immediately returning from the function.

For the 3rd task it wants you to return an iterable that contains both the first 4 and last 4.

The two slices can be added together to form a new iterable.

whaa? so I've been doing wrong? bummer. I thought for sure it needed a for loop because it said "iterable" but thank you very much for the heads up

Well an iterable is being passed in but you do not have to iterate over that iterable. The tasks are only asking you to return slices of that iterable.

thank you I've managed to solve it when you hinted me at "added" thank you very much

You're welcome. Glad you figured it out.