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

slices

make a function named odds that accept an iterable as an argument and returns the items with an odd index in the iterable.

im stuck guyz

slices.py
def first_4(iterable):
  return iterable[0:4]

first_4([66, 333, 222, 1, 1234])

5 Answers

Mikael Enarsson
Mikael Enarsson
7,056 Points

Ok, what you want to do is to make the slice start at 1, since it's the first odd number:

def odds(iterable):
   return iterable[1:       #Starting at position 1

You want it to run to the end, so you don't add a stop:

def odds(iterable):
   return iterable[1::      #No stoping until the end of the list

You want to take two steps every time, so:

def odds(iterable):
   return iterable[1::2]     #Step is two, so we get 1, 3, 5, 7 etc.

Should do it.

it is work for me

def first_4(iterable):
  return iterable[0:4]+ iterable[45:50]

def odds(iterable):
  return iterable[1::2]
job asfaw
job asfaw
4,729 Points

def odds(iterable): return iterable[1::2] this works if the list starts from 0 but this was not stated in the problem

My code is this, but still I'm getting error : Oops! It looks like Task 1 is no longer passing. def first_4(list1): return list1[:4]

def odds[list2]: return list2[1::2]

Mikael Enarsson
Mikael Enarsson
7,056 Points
some_iterable[(start position inclusive):(end position exclusive):(step length, i.e. 1 takes every number, 2 takes every second number etc.)]

where is odds

def odds (iterable): return iterable [0:1]

its failing to work guys, how have you go about it.