In this article, we will discuss different ways to fetch all items at even 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 even 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 even index position, start from index 0, 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 even index positions elements = listOfNumbers[ : : 2] print(elements)
Output :
[10, 12, 14, 16, 18]
Here, we selected items at even 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 even. 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 even index positions for index, value in enumerate(listOfNumbers): if index % 2 == 0: elements.append(value) print(elements)
Output :
[10, 12, 14, 16, 18]
Here, we selected items at even 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 even index positions elements = [value for index, value in enumerate(listOfNumbers) if index % 2 == 0] print(elements)
Output :
[10, 12, 14, 16, 18]
Here, we selected items at even 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 even index position in List. But to verify, if an index position is even or not, we will use the AND Operator. When we apply AND operator to an index position and number 1, if it returns 0, then it means index position is even. Let’s see an example,
# List of Numbers listOfNumbers = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19] # Get list elements at even index positions elements = [value for index, value in enumerate(listOfNumbers) if (index & 1) == 0] print(elements)
Output :
[10, 12, 14, 16, 18]
Here, we selected items at even index positions from the list.
Summary
We learned how to fetch items at even index positions of a List in Python.