List and Functions quiz question: Replace the line in the function so that the functions prints every other list item


#1

In the List and Functions quiz there's a question:

Replace the line in the function so that the functions prints every other list item.

def print_list(x):

  for counter in ________________:

  print x[counter]

With the following options:
range(0, x ,2)
range(0, len(x),1)
range(1, len(x), -2)
range(0, len(x),2)

With the right one being the last, range(0, len(x),2).

I have no idea why though, can someone explain?


#2

if you look at the documentation of range you see this:

range(start, stop, step)

well, you want every other item so it logic to start at zero, till the end (which is len(x) ) with steps of two (since you want every other item)


#3

Does step 3 take every third item? so 0 - 3 - 6 - 9?

Is there a particular use for it?


#4

that is the point of steps, isn't it?

well, if you have list with odd and even numbers it is really easy to extract even or odd numbers, or in steps of 4 to extract leap years.


#5

Thanks for the help! :smiley: