Related Articles

# Python | Counting Nth tuple element

• Last Updated : 03 Nov, 2019

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 expressionfrom collections import Counter  # initialize listtest_list = [('gfg', 0), ('is', 1), ('best', 2),             ('gfg', 2), ('is', 0), ('for', 1),             ('geeks', 2)]  # printing original listprint("The original list : " + str(test_list))  # initialize NN = 1  # Counting Nth tuple element# using Counter() + generator expressionres = dict(Counter(sub[N] for sub in test_list))  # printing resultprint("The grouped Nth element frequency is : " + str(res))
Output :

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 Counterfrom operator import itemgetter  # initialize listtest_list = [('gfg', 0), ('is', 1), ('best', 2),              ('gfg', 2), ('is', 0), ('for', 1),             ('geeks', 2)]  # printing original listprint("The original list : " + str(test_list))  # initialize NN = 1  # Counting Nth tuple element# using Counter() + map() + itemgetter()res = dict(Counter(map(itemgetter(N), test_list)))  # printing resultprint("The grouped Nth element frequency is : " + str(res))
Output :

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}

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