Skip to content
Related Articles

Related Articles

Python | Count tuples occurrence in list of tuples

Improve Article
Save Article
Like Article
  • 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})

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!