To begin, we introduce a method called every_nth_element. This receives a list, and the "Nth" value "n." So if we pass 2, we get every second element.
def every_nth_element(items, n):
results = []
# Loop over elements and indexes.
for i, value in enumerate(items):
# If evenly divisible, accept the element.
if i % n == 0:
results.append(value)
return results
# Input values.
values = [
"a",
"b",
"c",
"d",
"e",
"f",
"g",
"h",
"i"]
# Get every other element.
x1 = every_nth_element(values, 2)
print(x1)
# Get every third element.
x2 = every_nth_element(values, 3)
print(x2)
['a', 'c', 'e', 'g', 'i']
['a', 'd', 'g']