Python - List Frequency of Elements Last Updated : 10 Feb, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report We are given a list we need to count frequencies of all elements in given list. For example, n = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4] we need to count frequencies so that output should be {4: 4, 3: 3, 2: 2, 1: 1}.Using collections.Countercollections.Counter class provides a dictionary-like structure that counts occurrences of elements in a list. It returns a dictionary where keys are elements and values are their frequencies. Python from collections import Counter n = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4] # Creates a dictionary with element counts f = Counter(n) print(f) OutputCounter({4: 4, 3: 3, 2: 2, 1: 1}) Explanation:Counter(n) creates a frequency dictionary where each key is a unique element, and its value represents count of occurrences.This method is efficient and concise for counting elements in a list making it useful for frequency analysis.Using a Dictionary with get()A dictionary with the get() method can be used to count element frequencies by initializing counts to zero and updating them dynamically. The get() method helps retrieve values safely avoiding key errors when encountering new elements. Python n = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4] f = {} for num in n: # Increment count f[num] = f.get(num, 0) + 1 print(f) Output{1: 1, 2: 2, 3: 3, 4: 4} Explanation:get(num, 0) method initializes missing keys to 0 before incrementing, avoiding key errors.This approach manually builds a frequency dictionary by iterating over the list and counting occurrences.Using collections.defaultdictcollections.defaultdict provides a dictionary with a default value type eliminating need to check for missing keys. It simplifies frequency counting by automatically initializing values when a new key is encountered. Python from collections import defaultdict n = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4] f = defaultdict(int) for num in n: # Increment count f[num] += 1 print(dict(f)) Output{1: 1, 2: 2, 3: 3, 4: 4} Explanation:defaultdict(int) initializes missing keys with 0 automatically, avoiding the need for get().This method ensures efficient frequency counting with minimal code by leveraging defaultdict's auto-initialization. Comment More infoAdvertise with us Next Article Python - List Frequency of Elements M manjeet_04 Follow Improve Article Tags : Python Python Programs Python list-programs Practice Tags : python Similar Reads Python - Step Frequency of elements in List Sometimes, while working with Python, we can have a problem in which we need to compute frequency in list. This is quite common problem and can have usecase in many domains. But we can atimes have problem in which we need incremental count of elements in list. Let's discuss certain ways in which thi 4 min read Python | Frequency grouping of list elements Sometimes, while working with lists, we can have a problem in which we need to group element along with it's frequency in form of list of tuple. Let's discuss certain ways in which this task can be performed. Method #1: Using loop This is a brute force method to perform this particular task. In this 6 min read Frequency of Elements from Other List - Python We are given a list of elements and another list containing specific values and our task is to count the occurrences of these specific values in the first list "a" and return their frequencies. For example: a = [1, 2, 2, 3, 4, 2, 5, 3, 1], b = [1, 2, 3]. Here b contains the elements whose frequency 3 min read Sort List Elements by Frequency - Python Our task is to sort the list based on the frequency of each element. In this sorting process, elements that appear more frequently will be placed before those with lower frequency. For example, if we have: a = ["Aryan", "Harsh", "Aryan", "Kunal", "Harsh", "Aryan"] then the output should be: ['Aryan' 3 min read Python | Group list elements based on frequency Given a list of elements, write a Python program to group list elements and their respective frequency within a tuple. Examples: Input : [1, 3, 4, 4, 1, 5, 3, 1] Output : [(1, 3), (3, 2), (4, 2), (5, 1)] Input : ['x', 'a', 'x', 'y', 'a', 'x'] Output : [('x', 3), ('a', 2), ('y', 1)] Method #1: List c 5 min read Like