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


When it is required to print the elements of a list that is present in odd index/position, a loop can be used to iterate over the elements, and only check the odd 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(1, 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 :
42
34
0
1

Explanation

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

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

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

Updated on: 16-Apr-2021

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements