Skip to content
Related Articles
Python | Count tuples occurrence in list of tuples
• Last Updated : 03 Apr, 2019

Many a time while developing web and desktop product in Python, we use nested lists and have several queries about how to find count of unique tuples. Let’s see how to get count of unique tuples in given list of tuples.

Below are some ways to achieve the above task.

Method #1: Using Iteration

 `# Python code to count unique ``# tuples in list of list`` ` `import` `collections ``Output ``=` `collections.defaultdict(``int``)`` ` `# List initialization``Input` `=` `[[(``'hi'``, ``'bye'``)], [(``'Geeks'``, ``'forGeeks'``)],``         ``[(``'a'``, ``'b'``)], [(``'hi'``, ``'bye'``)], [(``'a'``, ``'b'``)]]`` ` `# Using iteration``for` `elem ``in` `Input``:``      ``Output[elem[``0``]] ``+``=` `1``     ` `# Printing output``print``(Output)`
Output:
```defaultdict(<class 'int'>, {('Geeks', 'forGeeks'): 1, ('hi', 'bye'): 2, ('a', 'b'): 2})
```

Method #2: Using `chain `and `Counter`

 `# Python code to count unique ``# tuples in list of list`` ` `# Importing``from` `collections ``import` `Counter``from` `itertools ``import` `chain`` ` `# List initialization``Input` `=` `[[(``'hi'``, ``'bye'``)], [(``'Geeks'``, ``'forGeeks'``)],``         ``[(``'a'``, ``'b'``)], [(``'hi'``, ``'bye'``)], [(``'a'``, ``'b'``)]]`` ` `# Using counter and chain``Output ``=` `Counter(chain(``*``Input``))`` ` `# Printing output``print``(Output)`
Output:
```Counter({('hi', 'bye'): 2, ('a', 'b'): 2, ('Geeks', 'forGeeks'): 1})
```

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