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

*chevron_right*

*filter_none*

**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)) ` |

*chevron_right*

*filter_none*

**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}

## Recommended Posts:

- Python - Extract Even elements in Nested Mixed Tuple
- Python - Type conversion in Nested and Mixed List
- Python | Convert mixed data types tuple list to string list
- Python | How to get unique elements in nested tuple
- Python - Extract String elements from Mixed Matrix
- Python | Check if a nested list is a subset of another nested list
- Python - Raise elements of tuple as power to another tuple
- Python | Pair and combine nested list to tuple list
- Python | Cummulative Nested Tuple Column Product
- Python - Remove nested records from tuple
- Python - Nested List to single value Tuple
- Python - Convert Nested Tuple to Custom Key Dictionary
- Python - Convert Nested dictionary to Mapped Tuple
- Python - Skew Nested Tuple Summation
- Python - Elements frequency in Tuple Matrix
- Python - Elements frequency in Tuple
- Python | Integer count in Mixed List
- Python | Find Mixed Combinations of string and list
- Python | Lexicographically smallest string in mixed list
- Python | Convert numeric String to integers in mixed List

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.