Skip to content
Related Articles
Python – Extract Even elements in Nested Mixed Tuple
• Last Updated : 03 Jul, 2020

Sometimes, while working with Python tuples, we can have a problem in which we need to get all the even elements from tuple. The tuples can be nested or mixed. This kind of problem can occur in data domains. Let’s discuss certain ways in which this task can be performed.

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

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

Method #1 : Using recursion + `isinstance() `+ loop
This is one of the ways in which this task can be performed. In this, we perform task of getting the element instance to be integer using isinstance() and function is recursed once tuple is encountered.

 `# Python3 code to demonstrate working of ``# Extract Even elements in Nested Mixed Tuple``# Using recursion + isinstance() + loop`` ` `# helper_fnc``def` `even_ele(test_tuple, even_fnc):``    ``res ``=` `tuple``()``    ``for` `ele ``in` `test_tuple:``        ``if` `isinstance``(ele, ``tuple``):``            ``res ``+``=` `(even_ele(ele, even_fnc), )``        ``elif` `even_fnc(ele):``            ``res ``+``=` `(ele, )``    ``return` `res`` ` `# initializing tuples``test_tuple ``=` `(``4``, ``5``, (``7``, ``6``, (``2``, ``4``)), ``6``, ``8``)`` ` `# printing original tuple``print``(``"The original tuple : "` `+` `str``(test_tuple))`` ` `# Extract Even elements in Nested Mixed Tuple``# Using recursion + isinstance() + loop``res ``=` `even_ele(test_tuple, ``lambda` `x: x ``%` `2` `=``=` `0``)`` ` `# printing result ``print``(``"Even elements of tuple : "` `+` `str``(res))`
Output :

```The original tuple : (4, 5, (7, 6, (2, 4)), 6, 8)
Even elements of tuple : (4, (6, (2, 4)), 6, 8)
```

Method #2 : Using recursion + `isinstance() `+ generator expresssion
This method perform this task in similar manner as above method. The different is just that it is shorthand to above method, does the job in one line using generator expression.

 `# Python3 code to demonstrate working of ``# Extract Even elements in Nested Mixed Tuple``# Using recursion + isinstance() + generator expresssion`` ` `# helper_fnc``def` `even_ele(test_tuple, even_fnc):``    ``return` `tuple``(even_ele(ele, even_fnc) ``if` `isinstance``(ele, ``tuple``) ``else` `ele``    ``for` `ele ``in` `test_tuple ``if` `isinstance``(ele, ``tuple``) ``or` `even_fnc(ele))`` ` `# initializing tuples``test_tuple ``=` `(``4``, ``5``, (``7``, ``6``, (``2``, ``4``)), ``6``, ``8``)`` ` `# printing original tuple``print``(``"The original tuple : "` `+` `str``(test_tuple))`` ` `# Extract Even elements in Nested Mixed Tuple``# Using recursion + isinstance() + generator expresssion``res ``=` `even_ele(test_tuple, ``lambda` `x: x ``%` `2` `=``=` `0``)`` ` `# printing result ``print``(``"Even elements of tuple : "` `+` `str``(res))`
Output :
```The original tuple : (4, 5, (7, 6, (2, 4)), 6, 8)
Even elements of tuple : (4, (6, (2, 4)), 6, 8)
```

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. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up