Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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?

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.