Python program to print the elements of an array present on even position



When it is required to print the elements of a list that are present at even index/position, a loop can be used to iterate over the elements, and only check the even positions in the list by specifying the step size as 2 in the range function.

Below is a demonstration of the same −

Example

 Live Demo

my_list = [31, 42, 13, 34, 85, 0, 99, 1, 3] print("The list is :") print(my_list) print("The elements in odd positions are : ") for i in range(0, len(my_list), 2):    print(my_list[i])

Output

The list is : [31, 42, 13, 34, 85, 0, 99, 1, 3] The elements in odd positions are : 31 13 85 99 3

Explanation

  • A list is defined, and is displayed on the console.

  • The list is iterated over beginning from the first index element, and the step size is mentioned as 2 in the range method.

  • Those elements that are in even positions are displayed on the console.

Updated on: 2021-04-16T11:57:47+05:30

656 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements