Python – Get Even indexed elements in Tuple

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().

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output :

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

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.