Related Articles

# Python – Elements Frequency in Mixed Nested Tuple

• Last Updated : 02 Jun, 2020

Sometimes, while working with Python data, we can have a problem in which we have data in form of nested and non nested form inside in a single tuple, and we wish to count the element frequency in them. This kind of problem can come in domains such as web development and Data Science. Let’s discuss certain ways in which this task can be performed.

Input : test_tuple = (5, (6, (7, 8, 6)))
Output : {5: 1, 6: 2, 7: 1, 8: 1}

Input : test_tuple = (5, 6, 7, 8)
Output : {5: 1, 6: 1, 7: 1, 8: 1}

Method #1 : Using recursion + loop
The solution of this problem involved two steps. In first, we perform flattening of tuple using recursion and then the counting is performed in brute force manner using loop.

 `# Python3 code to demonstrate working of ``# Elements Frequency in Mixed Nested Tuple``# Using recursion + loop`` ` `# helper_fnc``def` `flatten(test_tuple):``    ``for` `tup ``in` `test_tuple:``        ``if` `isinstance``(tup, ``tuple``):``            ``yield` `from` `flatten(tup)``        ``else``:``            ``yield` `tup`` ` `# initializing tuple``test_tuple ``=` `(``5``, ``6``, (``5``, ``6``), ``7``, (``8``, ``9``), ``9``)`` ` `# printing original tuple``print``(``"The original tuple : "` `+` `str``(test_tuple))`` ` `# Elements Frequency in Mixed Nested Tuple``# Using recursion + loop``res ``=` `{}``for` `ele ``in` `flatten(test_tuple):``    ``if` `ele ``not` `in` `res:``        ``res[ele] ``=` `0``    ``res[ele] ``+``=` `1`` ` `# printing result ``print``(``"The elements frequency : "` `+` `str``(res))`
Output :

```The original tuple : (5, 6, (5, 6), 7, (8, 9), 9)
The elements frequency : {5: 2, 6: 2, 7: 1, 8: 1, 9: 2}
```

Method #2 : Using `Counter()` + resursion
This is yet another way in which this problem can be solved. In this, we employ Counter() to perform the task of counting elements.

 `# Python3 code to demonstrate working of ``# Elements Frequency in Mixed Nested Tuple``# Using recursion + Counter()``from` `collections ``import` `Counter`` ` `# helper_fnc``def` `flatten(test_tuple):``    ``for` `tup ``in` `test_tuple:``        ``if` `isinstance``(tup, ``tuple``):``            ``yield` `from` `flatten(tup)``        ``else``:``            ``yield` `tup`` ` `# initializing tuple``test_tuple ``=` `(``5``, ``6``, (``5``, ``6``), ``7``, (``8``, ``9``), ``9``)`` ` `# printing original tuple``print``(``"The original tuple : "` `+` `str``(test_tuple))`` ` `# Elements Frequency in Mixed Nested Tuple``# Using recursion + Counter()``res ``=` `dict``(Counter(flatten(test_tuple)))`` ` `# printing result ``print``(``"The elements frequency : "` `+` `str``(res))`
Output :
```The original tuple : (5, 6, (5, 6), 7, (8, 9), 9)
The elements frequency : {5: 2, 6: 2, 7: 1, 8: 1, 9: 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. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up