VOOZH about

URL: https://www.geeksforgeeks.org/python/python-counting-nth-tuple-element/

⇱ Python | Counting Nth tuple element - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

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(). 

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. 

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.


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

  1. Extract Nth column values from a list of tuples(test_list) using a for loop and store in the variable x.
  2. Remove duplicates from x (using list(), set())and store it in y, creating an empty dictionary res.
  3. Initialize the dictionary res with values of y as keys and count of these keys in x as values(using count()).
  4. Display 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)

Comment