Why did my list slice not omit the even numbers?


#1

Question

Why did my list slice not omit the even numbers?

Answer

In this exercise we want to print only the odd numbers in the range. To do this we only need to change one of the slice indexes: the stride index. If our range starts at 1, that’s already an odd number, so printing every other number will print all of the odd numbers!
To print every other number, the example shown in the instructions shows us:

print to_five[::2]
# print ['A', 'C', 'E']

FAQ: Learn Python - Advanced Topics in Python - Omitting Indices