Junior
How to output each even element of a list?
sobes.tech AI
Answer from AI
# Create a list
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Use slicing with step 2, starting from the first element (index 1)
even_elements = my_list[1::2]
# Print the result
print(even_elements)
# Create a list
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Iterate over the list using range with step 2, starting from index 1
for i in range(1, len(my_list), 2):
# Print the element at the even index
print(my_list[i])