Python | Count tuples occurrence in list of tuples
Last Updated :
01 Jun, 2023
Many a time while developing web and desktop products in Python, we use nested lists and have several queries about how to find the count of unique tuples. Let us see how to get the count of unique tuples in the given list of tuples. Below are some ways to achieve the above task.
Method #1: Using Iteration
Python3
import collections
Output = collections.defaultdict( int )
Input = [[( 'hi' , 'bye' )], [( 'Geeks' , 'forGeeks' )],
[( 'a' , 'b' )], [( 'hi' , 'bye' )], [( 'a' , 'b' )]]
for elem in Input :
Output[elem[ 0 ]] + = 1
print (Output)
|
Output:
defaultdict(<class 'int'>, {('Geeks', 'forGeeks'): 1, ('hi', 'bye'): 2, ('a', 'b'): 2})
Time complexity: O(n), where n is the total number of tuples in the list of lists.
Auxiliary space: O(m), where m is the total number of unique tuples in the list of lists.
Method #2: Using chain and Counter
Python3
from collections import Counter
from itertools import chain
Input = [[( 'hi' , 'bye' )], [( 'Geeks' , 'forGeeks' )],
[( 'a' , 'b' )], [( 'hi' , 'bye' )], [( 'a' , 'b' )]]
Output = Counter(chain( * Input ))
print (Output)
|
Output:
Counter({('hi', 'bye'): 2, ('a', 'b'): 2, ('Geeks', 'forGeeks'): 1})
Time complexity: O(n), where n is the total number of tuples in the input list of lists.
Auxiliary space: O(n), as we are using a Counter object to store the counts of each unique tuple, which can take up to n space in the worst case.
Method #3: List Comprehension method
Python3
Input = [( 'hi' , 'bye' ),( 'Geeks' , 'forGeeks' ),( 'a' , 'b' ),( 'hi' , 'bye' ),( 'a' , 'b' )]
check_ele = ( 'a' , 'b' )
x = [i for i in Input if i = = check_ele]
print ( "tuple ('a', 'b') occurs" , len (x), "times" )
|
Output
tuple ('a', 'b') occurs 2 times
Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(k), where k is the number of occurrences of the check element in the input list.
Method #4: Using enumerate function
Python3
Input = [( 'hi' , 'bye' ),( 'Geeks' , 'forGeeks' ),( 'a' , 'b' ),( 'hi' , 'bye' ),( 'a' , 'b' )]
check_ele = ( 'a' , 'b' )
x = [i for a,i in enumerate ( Input ) if i = = check_ele]
print ( "tuple ('a', 'b') occurs" , len (x), "times" )
|
Output
tuple ('a', 'b') occurs 2 times
Time complexity: O(n), where n is the length of the input list ‘Input’.
Auxiliary space: O(m), where m is the number of occurrences of the tuple ‘check_ele’ in the input list ‘Input’
Method #5: Using lambda function
Python3
Input = [( 'hi' , 'bye' ),( 'Geeks' , 'forGeeks' ),( 'a' , 'b' ),( 'hi' , 'bye' ),( 'a' , 'b' )]
check_ele = ( 'a' , 'b' )
x = list ( filter ( lambda i:(i = = check_ele), Input ))
print ( "tuple ('a', 'b') occurs" , len (x), "times" )
|
Output
tuple ('a', 'b') occurs 2 times
Time complexity: O(n), where n is the number of tuples in the Input list.
Auxiliary space: O(1) because it uses only a constant amount of extra space to store the filtered list and the length of that list.
Method #6: Using only Counter function
Python3
from collections import Counter
Input = [( 'hi' , 'bye' ),( 'Geeks' , 'forGeeks' ),( 'a' , 'b' ),( 'hi' , 'bye' ),( 'a' , 'b' )]
check_ele = ( 'a' , 'b' )
x = Counter( Input )
print ( "tuple ('a', 'b') occurs" ,x[check_ele], "times" )
|
Output
tuple ('a', 'b') occurs 2 times
Time complexity: O(n), where n is the number of tuples in the Input list.
Auxiliary space: O(n), where n is the number of tuples in the Input list.
Method #7: Using countof function
Python3
import operator as op
Input = [( 'hi' , 'bye' ),( 'Geeks' , 'forGeeks' ),( 'a' , 'b' ),( 'hi' , 'bye' ),( 'a' , 'b' )]
check_ele = ( 'a' , 'b' )
print (op.countOf( Input ,check_ele))
|
Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(n), because the list comprehension creates a new list of all occurrences of the check_ele tuple.
Method #8: Using dict() method
Creating empty dictionary and iterating every element in list. Checking if element in dictionary increment value of key i by 1 else assign the key to element i with 1. finally, the dictionary will have keys as elements and their values as count of element in list.
Python3
Input = [( 'hi' , 'bye' ),( 'Geeks' , 'forGeeks' ),( 'a' , 'b' ),( 'hi' , 'bye' ),( 'a' , 'b' )]
count_tuples = dict ()
for i in Input :
if i not in count_tuples:
count_tuples[i] = 1
else :
count_tuples[i] + = 1
print (count_tuples)
|
Output
{('hi', 'bye'): 2, ('Geeks', 'forGeeks'): 1, ('a', 'b'): 2}
Time complexity: O(n), where n is the length of the input list. The for loop iterates over every element in the input list exactly once.
Auxiliary space: O(m), where m is the number of unique tuples in the input list. In the worst case, when all the tuples are unique, the dictionary will contain m key-value pairs.
Method #9: Using map() function and tuple() constructor
This method involves mapping each list of tuples to a tuple of tuples and then counting the occurrences of each tuple in the list.
Python3
Input = [[( 'hi' , 'bye' )], [( 'Geeks' , 'forGeeks' )],
[( 'a' , 'b' )], [( 'hi' , 'bye' )], [( 'a' , 'b' )]]
Output = {}
for elem in Input :
t = tuple ( map ( tuple , elem))
Output[t] = Output.get(t, 0 ) + 1
print (Output)
|
Output
{(('hi', 'bye'),): 2, (('Geeks', 'forGeeks'),): 1, (('a', 'b'),): 2}
Time Complexity: O(nk), where n is the number of elements in the input list and k is the maximum number of tuples in a sublist.
Auxiliary Space: O(n), where n is the number of elements in the input list.
Method #10: Using set() and count()
- Initializes a list of tuples input_list containing five tuples.
- Converts the list to a set unique_set to remove duplicate tuples.
- Creates a dictionary output_dict where each key is a unique tuple from input_list, and each value is the count of that tuple in input_list.
- Prints the dictionary output_dict.
Python3
input_list = [( 'hi' , 'bye' ),( 'Geeks' , 'forGeeks' ),( 'a' , 'b' ),( 'hi' , 'bye' ),( 'a' , 'b' )]
unique_set = set (input_list)
output_dict = {elem: input_list.count(elem) for elem in unique_set}
print (output_dict)
|
Output
{('a', 'b'): 2, ('hi', 'bye'): 2, ('Geeks', 'forGeeks'): 1}
The time complexity is O(n^2)
The auxiliary space is also O(n^2)
Please Login to comment...