 GeeksforGeeks App
Open App Browser
Continue

# Python | Count tuples occurrence in list of tuples

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

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

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

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

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

Output

`2`

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

 `#Initializing tuples in a list``Input` `=` `[(``'hi'``, ``'bye'``),(``'Geeks'``, ``'forGeeks'``),(``'a'``, ``'b'``),(``'hi'``, ``'bye'``),(``'a'``, ``'b'``)]``#creating empty dictionary ``count_tuples``=``dict``()``#using for loop to iterate every value in Input``for` `i ``in` `Input``:``  ``if` `i ``not` `in` `count_tuples:   ``#checking if element i present in dictionary  or not``    ``count_tuples[i]``=``1``  ``else``:``    ``count_tuples[i]``+``=``1``#printing dictionary  of elements and their count``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

 `# Python code to count unique``# tuples in list of list` `# List initialization``Input` `=` `[[(``'hi'``, ``'bye'``)], [(``'Geeks'``, ``'forGeeks'``)],``         ``[(``'a'``, ``'b'``)], [(``'hi'``, ``'bye'``)], [(``'a'``, ``'b'``)]]` `# Using map() function and tuple() constructor``Output ``=` `{}``for` `elem ``in` `Input``:``    ``t ``=` `tuple``(``map``(``tuple``, elem))``    ``Output[t] ``=` `Output.get(t, ``0``) ``+` `1` `# Printing output``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.

My Personal Notes arrow_drop_up