Python | Finding frequency in list of tuples
In python we need to handle various forms of data and one among them is list of tuples in which we may have to perform any kind of operation. This particular article discusses the ways of finding the frequency of the 1st element in list of tuple which can be extended to any index. Let’s discuss certain ways in which this can be performed.
Method #1 : Using map() + count()
The map function can be used to accumulate the indices of all the tuples in a list and the task of counting the frequency can be done using the generic count function of python library.
# Python3 code to demonstrate # finding frequency in list of tuples # using map() + count() # initializing list of tuples test_list = [('Geeks', 1), ('for', 2), ('Geeks', 3)] # printing the original list print ("The original list is : " + str(test_list)) # using map() + count() # finding frequency in list of tuples res = list(map(lambda i : i[0], test_list)).count('Geeks') # printing result print ("The frequency of element is : " + str(res)) |
The original list is : [('Geeks', 1), ('for', 2), ('Geeks', 3)]
The frequency of element is : 2
Method #2 : Using Counter() + list comprehension
List comprehension performs the task of getting the first element of the tuples and the counting part is handled by Counter function of collection library.
# Python3 code to demonstrate # finding frequency in list of tuples # using Counter() + list comprehension from collections import Counter # initializing list of tuples test_list = [('Geeks', 1), ('for', 2), ('Geeks', 3)] # printing the original list print ("The original list is : " + str(test_list)) # using Counter() + list comprehension # finding frequency in list of tuples res = Counter(i[0] for i in test_list) # printing result print ("The frequency of element is : " + str(res['Geeks'])) |
The original list is : [('Geeks', 1), ('for', 2), ('Geeks', 3)]
The frequency of element is : 2
Recommended Posts:
- Python | Remove duplicate tuples from list of tuples
- Python | Count tuples occurrence in list of tuples
- Python | Remove tuples from list of tuples if greater than n
- Python | Remove tuples having duplicate first value from given list of tuples
- Python | Find the tuples containing the given element from a list of tuples
- Python | Combining tuples in list of tuples
- Python program to create a list of tuples from given list having number and its cube in each tuple
- Python | Convert list of strings to list of tuples
- Python | Convert list of tuples to list of strings
- Python | Convert list of tuples to list of list
- Python | Update a list of tuples using another list
- Python | Convert list of tuples into list
- Python | Min and Max value in list of tuples
- Python | Unzip a list of tuples
- Python | Group tuples in list with same first value
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.



