Python | Counting Nth tuple element
Sometimes, while working with Python, we can have a problem in which we need to count the occurrence of a particular’s elements. This kind of problem is quite common while working with records. Let’s discuss a way in which this task can be performed.
Method #1 : Using Counter() + generator expression
The combination of above functionalities can be used to achieve this particular task. In this, we iterate through a specific index using generator expression and compute the count using Counter().
# Python3 code to demonstrate working of # Counting Nth tuple element # using Counter() + generator expression from collections import Counter # initialize list test_list = [('gfg', 0), ('is', 1), ('best', 2), ('gfg', 2), ('is', 0), ('for', 1), ('geeks', 2)] # printing original list print("The original list : " + str(test_list)) # initialize N N = 1 # Counting Nth tuple element # using Counter() + generator expression res = dict(Counter(sub[N] for sub in test_list)) # printing result print("The grouped Nth element frequency is : " + str(res)) |
The original list : [(‘gfg’, 0), (‘is’, 1), (‘best’, 2), (‘gfg’, 2), (‘is’, 0), (‘for’, 1), (‘geeks’, 2)]
The grouped Nth element frequency is : {0: 2, 1: 2, 2: 3}
Method #2 : Using Counter() + map() + itemgetter()
The combination of above functions can be used to achieve this task. In this, the task performed by generator expression is performed by map() and itemgetter() is used to get the index of the container element.
# Python3 code to demonstrate working of # Counting Nth tuple element # using Counter() + map() + itemgetter() from collections import Counter from operator import itemgetter # initialize list test_list = [('gfg', 0), ('is', 1), ('best', 2), ('gfg', 2), ('is', 0), ('for', 1), ('geeks', 2)] # printing original list print("The original list : " + str(test_list)) # initialize N N = 1 # Counting Nth tuple element # using Counter() + map() + itemgetter() res = dict(Counter(map(itemgetter(N), test_list))) # printing result print("The grouped Nth element frequency is : " + str(res)) |
The original list : [(‘gfg’, 0), (‘is’, 1), (‘best’, 2), (‘gfg’, 2), (‘is’, 0), (‘for’, 1), (‘geeks’, 2)]
The grouped Nth element frequency is : {0: 2, 1: 2, 2: 3}
Recommended Posts:
- Python | Sort tuple list by Nth element of tuple
- Python | Replace tuple according to Nth tuple element
- Python | Adding N to Kth tuple element
- Python | Get tuple element data types
- Python | Minimum element in tuple list
- Python | Update each element in tuple list
- Python | Count occurrences of an element in a Tuple
- Python | Sort a tuple by its float element
- Python | Check if element is present in tuple
- Python | Remove particular element from tuple list
- Python | Maximum element in tuple list
- Python | Count the elements in a list until an element is a Tuple
- Python | Check if element is present in tuple of tuples
- Access front and rear element of Python tuple
- Python | Sort tuple based on occurrence of first element
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.


