Python - Get Even indexed elements in Tuple

Getting even-indexed elements from a tuple means extracting elements at positions 0, 2, 4, 6, etc. Python provides several efficient approaches to accomplish this task using slicing, list comprehension, and functional programming methods.

Using range() with List Conversion

We can iterate through the tuple using range() with a step of 2, collect even-indexed elements in a list, then convert back to a tuple ?

def get_even_elements(t):
    even_elements = []
    for i in range(0, len(t), 2):
        even_elements.append(t[i])
    return tuple(even_elements)

t = (12, 78, 4, 5, 6, 52, 48, 2, 8, 658, 585)
print(f"The even indexed elements are: {get_even_elements(t)}")
The even indexed elements are: (12, 4, 6, 48, 8, 585)

Using Tuple Slicing

Slicing with step 2 is the most concise and efficient method. The syntax [start:stop:step] allows us to extract every second element starting from index 0 ?

Syntax

<iterable>[<start index> : <stop index (exclusive)> : step]

Example

def get_even_elements(t):
    return t[::2]

t = (42, 7, 8, 5, 4, 6, 2, 3, 5, 4, 8)
print(f"The even indexed elements are: {get_even_elements(t)}")
The even indexed elements are: (42, 8, 4, 2, 5, 8)

Using List Comprehension

List comprehension provides a readable one-liner solution that creates a list of even-indexed elements, then converts it to a tuple ?

def get_even_elements(t):
    return tuple([t[i] for i in range(0, len(t), 2)])

t = (45, 2, 4, 7, 5, 35, 89, 56, 12, 74, 2, 45, 774)
print(f"The even indexed elements are: {get_even_elements(t)}")
The even indexed elements are: (45, 4, 5, 89, 12, 2, 774)

Using Lambda with reduce()

The reduce() function from functools can build a new tuple by accumulating even-indexed elements ?

from functools import reduce

def get_even_elements(t):
    return reduce(lambda acc, x: acc + (x,), t[::2], ())

t = (10, 20, 30, 40, 50)
print(f"The even indexed elements are: {get_even_elements(t)}")
The even indexed elements are: (10, 30, 50)

Using filter() and map()

We can filter even indices first, then map them to their corresponding tuple elements ?

def get_even_elements(t):
    return tuple(map(lambda i: t[i], filter(lambda i: i % 2 == 0, range(len(t)))))

t = (10, 20, 30, 40, 50)
print(f"The even indexed elements are: {get_even_elements(t)}")
The even indexed elements are: (10, 30, 50)

Comparison

Method Readability Performance Best For
Slicing t[::2] High Best Most cases
List Comprehension High Good Complex conditions
range() Loop Medium Good Step-by-step logic
filter() + map() Low Fair Functional programming

Conclusion

Tuple slicing with t[::2] is the most efficient and readable method for extracting even-indexed elements. Use list comprehension when you need additional conditions, and functional approaches like filter() and map() when working in a functional programming style.

Updated on: 2026-03-27T08:29:56+05:30

574 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements