Code Challenge Loops

Can someone explain to me why this works, I don’t quite understand how it’s pulling the odd indexes to be printed. I don’t understand what that range is doing?

Create a function named odd_indices() that has one parameter named lst .

The function should create a new empty list and add every element from lst that has an odd index. The function should then return this new list.

For example, odd_indices([4, 3, 7, 10, 11, -2]) should return the list [3, 10, -2] .

def odd_indices(lst):
new_lst =
for index in range(1, len(lst), 2):

print(odd_indices([4, 3, 7, 10, 11, -2]))

Are you familiar with how the range() function works? You may want to review it as it is rather important when working with loops. It creates a list like object based on 1 to three arguments.

range(value_to_start_with, value_to_end_at, value_to_step_by)

# or in your case

range(1, len(lst), 2)

If your first value, or the number you are starting, with is 1, and the maximum value that can be in the range is len(lst), than by stepping 2 integers, you are creating a list like object containing every second number, starting with 1 through the length of your list.

Essentially you can think of it like this, take two numbers and make a list containing every integer from one to the other:

# The second argument or len(lst)
#           \
[1, 2, 3, 4, 5]
# \
#  The first arguement

Now starting with the first number in the list only keep every second number:

[1, 3, 5]

Now you have a list of odd indexes to loop through.