Open In App

Python – Get Even indexed elements in Tuple

Last Updated : 05 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with Python data, one can have a problem in which we need to perform the task of extracting just even indexed elements in tuples. This kind of problem is quite common and can have possible application in many domains such as day-day programming. Let’s discuss certain ways in which this task can be performed.

Input : test_tuple = (1, 2, 4, 5, 6) Output : (1, 4, 6) Input : test_tuple = (1, 2, 4) Output : (1, 4)

Method #1 : Using tuple() + generator expression + enumerate() The combination of above functions can be used to solve this problem. In this, we perform task of iteration using generator expression, checking for even index using enumerate() and converting result to tuple using tuple(). 

Python3




# Python3 code to demonstrate working of
# Extract Even indexed elements in Tuple
# Using tuple() + generator expression + enumerate()
 
# initializing tuples
test_tuple = (5, 'Gfg', 2, 8.8, 1.2, 'is')
 
# printing original tuple
print("The original tuple : " + str(test_tuple))
 
# Extract Even indexed elements in Tuple
# Using tuple() + generator expression + enumerate()
res = tuple(ele for idx, ele in enumerate(test_tuple) if idx % 2 == 0)
 
# printing result
print("The even indexed elements : " + str(res))


Output : 

The original tuple : (5, 'Gfg', 2, 8.8, 1.2, 'is')
The even indexed elements : (5, 2, 1.2)

  Method #2 : Using recursion This is yet another way in which this task can be performed. In this, we pass into recur. function the new list and extract the initial element and pass the list again till end of list, extracting just the even indexed element. 

Python3




# Python3 code to demonstrate working of
# Extract Even indexed elements in Tuple
# Using recursion
 
def helper_fnc(test_tuple):
    if len(test_tuple) == 0 or len(test_tuple) == 1:
        return ()
    return (test_tuple[0], ) + helper_fnc(test_tuple[2:])
 
# initializing tuples
test_tuple = (5, 'Gfg', 2, 8.8, 1.2, 'is')
 
# printing original tuple
print("The original tuple : " + str(test_tuple))
 
# Extract Even indexed elements in Tuple
# Using recursion
res = helper_fnc(test_tuple)
 
# printing result
print("The even indexed elements : " + str(res))


Output : 

The original tuple : (5, 'Gfg', 2, 8.8, 1.2, 'is')
The even indexed elements : (5, 2, 1.2)

Method #3: Using filter() function and lambda function:

Step-by-step Algorithm:

  1. Create a tuple test_tuple with some elements.
  2. Use the filter() function along with a lambda function to filter the elements in the tuple based on the condition that the index of the element is even.
  3. Create a new tuple res with the filtered elements.
  4. Print the final result.

Python3




test_tuple = (5, 'Gfg', 2, 8.8, 1.2, 'is')
print("The original elements are:" + str(test_tuple))
res = tuple(filter(lambda x: test_tuple.index(x) % 2 == 0, test_tuple))
print("The even indexed elements : " + str(res))


Output

The original elements are:(5, 'Gfg', 2, 8.8, 1.2, 'is')
The even indexed elements : (5, 2, 1.2)

Time complexity: O(n^2) 

This is because the index() method is called inside the lambda function to find the index of each element. The index() method has a time complexity of O(n) and it is called for each element in the tuple. Therefore, the overall time complexity is O(n^2).

Auxiliary Space: O(n)

This is because we are creating a new tuple res with the filtered elements. 

Method #4:  Using tuple slicing to get the even-indexed elements:

  • Initialize the input tuple.
  • Print the original tuple.
  • Use tuple slicing to extract the even-indexed elements from the tuple.
  • Print the resulting tuple.

Python3




# Python code to demonstrate working of
# Extract Even indexed elements in Tuple
# Using tuple slicing
 
# initializing tuples
test_tuple = (5, 'Gfg', 2, 8.8, 1.2, 'is')
 
# printing original tuple
print("The original tuple : " + str(test_tuple))
 
# Extract Even indexed elements in Tuple
# Using tuple slicing
res = test_tuple[::2]
 
# printing result
print("The even indexed elements : " + str(res))


Output

The original tuple : (5, 'Gfg', 2, 8.8, 1.2, 'is')
The even indexed elements : (5, 2, 1.2)

Time Complexity: O(1) as tuple slicing is a constant time operation.

Space Complexity: O(N) where N is the length of the original tuple.

Method #5: Using a for loop and range function to iterate over even indices

Initialize an empty list to store even-indexed elements.
Use a for loop and range function to iterate over even indices of the tuple.
Use the append() method to add the even-indexed elements to the empty list.
Convert the list to a tuple.
Print the resulting tuple.

Python3




# initializing tuples
test_tuple = (5, 'Gfg', 2, 8.8, 1.2, 'is')
 
# printing original tuple
print("The original tuple : " + str(test_tuple))
 
# Extract Even indexed elements in Tuple
# Using a for loop and range function
even_indices = []
for i in range(0, len(test_tuple), 2):
    even_indices.append(test_tuple[i])
res = tuple(even_indices)
 
# printing result
print("The even indexed elements : " + str(res))


Output

The original tuple : (5, 'Gfg', 2, 8.8, 1.2, 'is')
The even indexed elements : (5, 2, 1.2)

Time complexity: O(n)
Auxiliary space: O(n) (for the list used to store even-indexed elements)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads