Python - Get Even indexed elements in Tuple


The Tuple is one of the most important data types in Python. They are exclusively used as the data structures to pass sequential data as the parameters of any method. Indexing refers to accessing the elements of the sequential data through the index. In this article, we will learn how to get an even index in elements of Tuple.

Use The List And range Expression.

The mutable lists sequences of data separated by "," and enclosed by "[ ]". Python allows the conversion between the lists and the Tuples through the in-built methods. We can traverse the lists and append-only those elements at the even index. Next, we can use the tuple method of Python to convert the lists to tuple objects. Note that this method will not affect the existing tuple object, and hence new tuple object will be created.

Example

In the following example, we first created a user-defined function named get_even_elements. The function takes only one argument, which is a tuple. Next, we created an empty list in our function and iterated through the list with a step value of 2. This ensures that we only take the even indexed elements. For each iteration, we append the tuple element to the list. We used the tuple method to convert the list into a tuple and returned the result.

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)}")

Output

The even indexed elements are: (12, 4, 6, 48, 8, 585)

Using The Slicing Property

Slicing is a more convenient way to access the elements of iterable objects. It takes three important parameters: start (inclusive), end (exclusive), and step. For our goal, we can set the step to be 2 to get only the even indexed elements. If we do not exclusively set the start and stop, all the elements would be iterated.

Syntax

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

We can apply the syntax in any iterable objects. Here the start index is the index with which we need to start indexing. This will be inclusive. stop index is the index just before we need to stop the indexing. the step is the gap we want between each of the elements.

Example

In the following code, we have created the get_even_elements function, which takes a tuple as the argument. Next, it uses the slicing technique to return the sliced tuple with all the even indexed elements. We printed the result after that.

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)}")

Output

The even indexed elements are: (42, 8, 4, 2, 5, 8)

Using List Comprehension

List comprehension is a very convenient way to create list elements using expressions, conditional statements, etc., in a single line of code. We use the first append all the even indexed elements from the tuple and append it into the list first. Later we can convert it into a tuple using the tuple method.

Example

In the following code, we created the get_even_elements function. It is a void function and returns a tuple object. We used the list comprehension and range methods to append all the even indexed elements into the list. Next, we have used the tuple method to convert the list into 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)}")

Output

The even indexed elements are: (45, 4, 5, 89, 12, 2, 774)

Using Lambda And Reduce Method

A lambda function is also known as an anonymous function. The advantage of using this function is combining single expressions to create small tasks. This is particularly useful when we need to perform some small function, but we are sure that the function won't be used elsewhere.

Syntax

lambda arguments: expression

Lambda functions can take any number of arguments but only have one expression. They are often used with other in-built functions, such as map(), filter(), and reduce(), to perform quick and simple operations.

The reduce method is a part of the functool module in Python. It takes the function name and an iterable object. It helps to apply any valid function to all the elements of the iterable object. It is applied to the elements of the iterable pairwise until a single value is obtained.

Syntax

reduce(function, iterable)

Here the function represents the name of the function which we need to apply to all the elements of the iterable object. The iterable object can be a list, tuple, etc.

Example

In the following code, we first imported the reduce method from the functools module. Next, we created the get_even_elements function. The function takes the tuple as the argument. We used the lambda and the reduce method to access all the even indexed elements. We created a sample tuple and called the get_even_elements function. We printed the result using the print function and string formatting.

from functools import reduce
def get_even_elements(t):
    return reduce(lambda acc, x: acc + (x,), t[::2], ())
t = (10, 20, 30, 40, 50)
even_elements = get_even_elements(t)
print(f"The even indexed elements are: {get_even_elements(t)}")

Output

The even indexed elements are: (10, 30, 50)

Using Filter Map And Lambda Function

The map method is another powerful tool to apply any function to all the data elements. It allows you to create a new iterable object containing the results of applying the function to each element in the original iterable.

Example

In the following code, we have first created the get_even_elements function that takes a tuple object as the argument. Next, we used the filter method to filter out only those elements which are even indexed in the tuple object. We used the map method to map all the elements to the filter method. Next, we used the tuple method to convert the list into a tuple object.

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)
even_elements = get_even_elements(t)
print(f"The even indexed elements are: {get_even_elements(t)}")

Output

The even indexed elements are: (10, 30, 50)

Conclusion

In this article, we talked about various techniques which we can use to get even indexed elements in Python. Python allows several methods to achieve the same. We can use the indexing method and the list to get the even indexed elements. However, Python offers other methods like filter, map, reduce, etc., which we can use to get the even indexed elements more conveniently.

Updated on: 18-Jul-2023

94 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements