Python - Occurrence counter in List of Records Last Updated : 08 Mar, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report Sometimes, while dealing with records we can have a problem in which we need count the occurrence of incoming digits corresponding to different characters/players in a game and compile them in a dictionary. This can have application in gaming and web development. Lets discuss a way in which this can be done. Method : Using loop + Counter() The combination of above functions can be used to perform this task. In this, we iterate for the elements and compute frequency using Counter() and unique characters are managed by keys of dictionary. Python3 # Python3 code to demonstrate # Occurrence counter in List of Records # using Counter() + loop from collections import Counter # Initializing list test_list = [('Gfg', 1), ('Gfg', 2), ('Gfg', 3), ('Gfg', 1), ('Gfg', 2), ('is', 1), ('is', 2)] # printing original list print("The original list is : " + str(test_list)) # Occurrence counter in List of Records # using Counter() + loop res = {} for key, val in test_list: res[key] = [val] if key not in res else res[key] + [val] res = {key: dict(Counter(val)) for key, val in res.items()} # printing result print ("Mapped resultant dictionary : " + str(res)) Output : The original list is : [('Gfg', 1), ('Gfg', 2), ('Gfg', 3), ('Gfg', 1), ('Gfg', 2), ('is', 1), ('is', 2)] Mapped resultant dictionary : {'is': {1: 1, 2: 1}, 'Gfg': {1: 2, 2: 2, 3: 1}} Time Complexity: O(n*n) where n is the number of elements in the list “test_list”. Auxiliary Space: O(n) where n is the number of elements in the list “test_list”. Comment More infoAdvertise with us Next Article Python | Count tuples occurrence in list of tuples M manjeet_04 Follow Improve Article Tags : Python Python Programs Python list-programs Practice Tags : python Similar Reads Python | Count tuples occurrence in list of tuples Many a time while developing web and desktop products in Python, we use nested lists and have several queries about how to find the count of unique tuples. Let us see how to get the count of unique tuples in the given list of tuples. Below are some ways to achieve the above task. Method #1: Using It 5 min read Python - Count occurrences of each word in given text file Many times it is required to count the occurrence of each word in a text file. To achieve so, we make use of a dictionary object that stores the word as the key and its count as the corresponding value. We iterate through each word in the file and add it to the dictionary with a count of 1. If the w 4 min read Python | Count occurrence of all elements of list in a tuple Given a tuple and a list as input, write a Python program to count the occurrences of all items of the list in the tuple. Examples: Input : tuple = ('a', 'a', 'c', 'b', 'd') list = ['a', 'b'] Output : 3 Input : tuple = (1, 2, 3, 1, 4, 6, 7, 1, 4) list = [1, 4, 7] Output : 6 Approach #1 : Naive Appro 5 min read Element Occurrence in Dictionary of List Values - Python We are having a dictionary of list we need to find occurrence of all elements. For example, d = {'a': [1, 2, 3, 1], 'b': [3, 4, 1], 'c': [1, 5, 6]} we need to count the occurrence of all elements in dictionary list so that resultant output should be {'a': 2, 'b': 1, 'c': 1}.Using a Dictionary Compre 3 min read Python - Sum of each List element occurrence in another Sometimes, while working with Python, we can have a problem in which we need to get occurrence of 1 element in another. But as a modification of this, we can have a problem in which we need to count the occurrence of all elements of 1 list in another. Lets discuss certain ways in which this task can 6 min read Count Occurance of Substring in a List of Strings - Python To count the occurrences of a particular substring in a list of strings in Python, we can use several methods. In this article, we are going to explore different methods to count the existence of a particular substring in a given list.Using sum() and Generator ExpressionThis method uses a generator 2 min read Like