Get all items at odd indices of a List in Python

In this article, we will discuss different ways to fetch all items at odd index positions of a List in Python.

Table Of Contents

Method 1: Using List Slicing

We can slice the list using subscript operator to select elements at odd index positions only. To slice a list, we need three values,

list[start : end: N]

Here, start and end are index positions. Whereas, N is the step size. It returns, every Nth element from start till end-1. So, to select element at every odd index position, start from index 1, till the end of string, and keep the step size as 2. Let’s see an exaple,

# List of Numbers
listOfNumbers = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

# Get list elements at odd index positions
elements = listOfNumbers[ 1 : : 2]

print(elements)

Output :

Advertisements
[11, 13, 15, 17, 19]

Here, we selected items at odd index positions from the list.

Method 2: Using enumerate()

The enumerate() function accepts a sequence as arguments, and returns an iterable enumerate object. Which, on iteration, yields pairs containing index position, and value. So, we can pass our list as an argument to the enumerate() function, and iterate over pairs yielded by it. Each pair will have an index position & value at that index position in list. Select only those values for which index is odd. Let’s see an example,

# List of Numbers
listOfNumbers = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

# Empty List
elements = []

# Get list elements at odd index positions
for index, value in enumerate(listOfNumbers):
    if index % 2 == 1:
        elements.append(value)

print(elements)

Output :

[11, 13, 15, 17, 19]

Here, we selected items at odd index positions from the list.

Method 3: using List Comprehension

This solution is similar to the previous one, but with just one difference. Although, we will use the enumerate() function to iterate over all values of list, by index position. But we will use the List comprehension instead of direct for loop for iteration, and creation of new list. Let’s see an example,

# List of Numbers
listOfNumbers = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

# Get list elements at odd index positions
elements = [value for index, value in enumerate(listOfNumbers) if index % 2 == 1]

print(elements)

Output :

[11, 13, 15, 17, 19]

Here, we selected items at odd index positions from the list.

Method 4: using List Comprehension & AND operator

This solution is similar to the previous one, but with just one difference. Although, we will use the enumerate() function and List comprehension to select elements at odd index position in List. But to verify, if an index position is odd or not, we will use the AND Operator. When we apply AND operator to an index position and number 1, if it returns 1, then it means index position is ODD. Let’s see an example,

# List of Numbers
listOfNumbers = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

# Get list elements at odd index positions
elements = [value for index, value in enumerate(listOfNumbers) if index & 1]

print(elements)

Output :

[11, 13, 15, 17, 19]

Here, we selected items at odd index positions from the list.

Summary

We learned how to fetch items at odd index positions of a List in Python.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top