Python | Counting Nth tuple element
Last Updated :
21 Apr, 2023
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
from collections import Counter
test_list = [( 'gfg' , 0 ), ( 'is' , 1 ), ( 'best' , 2 ),
( 'gfg' , 2 ), ( 'is' , 0 ), ( 'for' , 1 ),
( 'geeks' , 2 )]
print ("The original list : " + str (test_list))
N = 1
res = dict (Counter(sub[N] for sub in test_list))
print ("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}
Time Complexity: O(n*n) where n is the number of elements in the list “test_list”. Counter() + generator expression performs n*n number of operations.
Auxiliary Space: O(n), extra space is required where n is the number of elements in the list
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
from collections import Counter
from operator import itemgetter
test_list = [( 'gfg' , 0 ), ( 'is' , 1 ), ( 'best' , 2 ),
( 'gfg' , 2 ), ( 'is' , 0 ), ( 'for' , 1 ),
( 'geeks' , 2 )]
print ("The original list : " + str (test_list))
N = 1
res = dict (Counter( map (itemgetter(N), test_list)))
print ("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}
Time Complexity: O(n*n), where n is the length of the list test_list
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list
Method #3: Using for loop and dictionary ().
we use a for loop and a dictionary to count the frequency of the N th element of each tuple in a given list of tuples. The input list, “test_list,” contains tuples with various words as the first element and a number as the N th element. The for loop iterates over each tuple in the list, and if the N th element of the tuple is already a key in the dictionary “dic,” the value associated with that key is incremented by one. If the N th element of the tuple is not a key in the dictionary, a new key is created with a value of one. After the for loop completes, the dictionary “dic” contains the frequency of each N th element in the input list, and this is printed to the console.
Python3
from collections import Counter
from operator import itemgetter
test_list = [( 'gfg' , 0 ), ( 'is' , 1 ), ( 'best' , 2 ),
( 'gfg' , 2 ), ( 'is' , 0 ), ( 'for' , 1 ),
( 'geeks' , 2 )]
print ( 'The original list :' + str (test_list))
N = 1
dic = {}
for i in test_list:
if i[N] in dic:
dic[i[N]] + = 1
else :
dic[i[N]] = 1
print ( 'The grouped Nth element frequency is : ' + str (dic))
|
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}
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #4 : Using count(), list(), set() methods
- Extract Nth column values from a list of tuples(test_list) using a for loop and store in the variable x.
- Remove duplicates from x (using list(), set())and store it in y, creating an empty dictionary res.
- Initialize the dictionary res with values of y as keys and count of these keys in x as values(using count()).
- Display res.
Python3
test_list = [( 'gfg' , 0 ), ( 'is' , 1 ), ( 'best' , 2 ),
( 'gfg' , 2 ), ( 'is' , 0 ), ( 'for' , 1 ),
( 'geeks' , 2 )]
print ( "The original list : " + str (test_list))
N = 1
res = dict ()
x = []
for i in test_list:
x.append(i[N])
y = list ( set (x))
for i in y:
res[i] = x.count(i)
print ( "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}
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...