List frequency of elements in PythonPythonServer Side ProgrammingProgramming In this article, we are going to learn how to find the frequency of elements in a list. We can solve the problem in different ways. Let's see two of them. Show
Follow the below steps to write the code.
Python: Get the frequency of the elements in a listLast update on April 13 2021 05:36:53 (UTC/GMT +8 hours)Counting the frequencies in a list using dictionary in PythonGiven an unsorted list of some elements(may or may not be integers), Find the frequency of each distinct element in the list using a dictionary. Recommended: Please try your approach on {IDE} first, before moving on to the solution. The problem can be solved in many ways. A simple approach would be to iterate over the list and use each distinct element of the list as a key of the dictionary and store the corresponding count of that key as values. Below is the Python code for this approach: Python
Output:
1 : 5
2 : 4
3 : 3
4 : 3
5 : 2
Time Complexity:O(N), where N is the length of the list. Alternative way: An alternative approach can be to use the list.count() method. Python
Output:
1 : 5
2 : 4
3 : 3
4 : 3
5 : 2
Time Complexity:O(N2), where N is the length of the list. The time complexity list.count() is O(N) alone, and when used inside loop it will become O(N2). Alternative way:An alternative approach can be to use the dict.get() method. This makes the program much more shorter and makes understand how get method is useful instead of if…else. Python
Output:
{1: 5, 5: 2, 3: 3, 4: 3, 2: 4}
Related Article : Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics. To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
Article Tags :
Python
frequency-counting Python dictionary-programs python-dict python-list Practice Tags :
python-dict python-list 2. Python program to find the frequency of each element in the arrayIn this program, we have an array of elements to count the occurrence of its each element. One of the approaches to resolve this problem is to maintain one array to store the counts of each element of the array. Loop through the array and count the occurrence of each element as frequency and store it in another array fr. In the given array, 1 has appeared two times, so its frequency is 2, and 2 has appeared four times so have frequency 4 and so on. ALGORITHM:
PROGRAM:Output: ----------------------------------------
Element | Frequency
----------------------------------------
1 | 2
2 | 4
8 | 1
3 | 1
5 | 1
----------------------------------------
Counting the frequencies in a list using dictionary in PythonIn this tutorial, you will learn to write a program in Python that will count the frequency of elements in a list using a dictionary. For a given list with multiple elements, we have to count the occurrence of each element in the list and display it as output. This can be done by creating a dictionary and using some built-in methods which will be discussed below. Look at the input-output format to understand better. Input: ['a', 'a', 'a', 'b','b'] Output: {'a': 3, 'b': 2} To solve this problem we can follow these approaches-
“count frequency of elements in list python” Code Answer’show to get frequency of each elements in a python list python by Frantic Ferret on May 24 2021 Comment 9 Source: buggyprogrammer.com python count the frequency of words in a list python by Andrea Perlato on Feb 25 2021 Donate Comment 1 Source: stackoverflow.com list count frequency python python by Cheerful Cormorant on Jul 15 2020 Comment 1 Source: stackoverflow.com Add a Grepper Answer Python answers related to “count frequency of elements in list python”
Python queries related to “count frequency of elements in list python”
Count frequency of elements in a list using for loopWe can count the frequency of elements in a list using a python dictionary. To perform this operation, we will create a dictionary that will contain unique elements from the input list as keys and their count as values. To count the frequency of elements in the list, first, we will create an empty dictionary. After that, for each new element, we will count the frequency of the element in the list. After getting the frequency of the element, we will add the element and its frequency as key-value pair to the dictionary. We will also maintain a list of visited elements to filter out the elements that have been already visited. This can be done as follows. myList = [1, 2, 3, 4, 1, 3, 46, 7, 2, 3, 5, 6, 10] frequencyDict = dict() visited = set() listLength = len(myList) for i in range(listLength): if myList[i] in visited: continue else: count = 0 element = myList[i] visited.add(myList[i]) for j in range(listLength - i): if myList[j+i] == element: count += 1 frequencyDict[element] = count print("Input list is:", myList) print("Frequency of elements is:") print(frequencyDict)Output: In the above example, we are iterating the whole list for each unique element. This makes the algorithm inefficient. In worst cases, when the list has all unique elements, we will have to process all the elements at least n*(n+1)/2 times where n is the length of the list. To overcome this shortcoming, we will modify the above algorithm. As an improvement, we will iterate over the list only once. To count the frequency of elements, we will iterate through the list and check for each element if it is already present as a key in the dictionary or not. If the current element is already present as a key in the dictionary, we will increment the count associated with that element by 1. If the current element is not already present as a key in the dictionary, we will add a new item to the dictionary with the current element as key and 1 as its associated value. We can implement this algorithm as follows. myList = [1, 2, 3, 4, 1, 3, 46, 7, 2, 3, 5, 6, 10] frequencyDict = dict() visited = set() for element in myList: if element in visited: frequencyDict[element] = frequencyDict[element] + 1 else: frequencyDict[element] = 1 visited.add(element) print("Input list is:", myList) print("Frequency of elements is:") print(frequencyDict)Output: Input list is: [1, 2, 3, 4, 1, 3, 46, 7, 2, 3, 5, 6, 10] Frequency of elements is: {1: 2, 2: 2, 3: 3, 4: 1, 46: 1, 7: 1, 5: 1, 6: 1, 10: 1}Improvisation to the above algorithm can be made using python try except blocks. In this method, we will use the try block to increment the frequency of the elements. Whenever an element is not already present in the dictionary, a KeyError will be raised. This will mean that the element is a unique element and its count hasn’t been added to the dictionary. In the except block, we will catch the KeyError and add a new key-value pair to the dictionary with the current element as key and 1 as its associated value. This will be done as follows. myList = [1, 2, 3, 4, 1, 3, 46, 7, 2, 3, 5, 6, 10] frequencyDict = dict() for element in myList: try: frequencyDict[element] = frequencyDict[element] + 1 except KeyError: frequencyDict[element] = 1 print("Input list is:", myList) print("Frequency of elements is:") print(frequencyDict)Output: Input list is: [1, 2, 3, 4, 1, 3, 46, 7, 2, 3, 5, 6, 10] Frequency of elements is: {1: 2, 2: 2, 3: 3, 4: 1, 46: 1, 7: 1, 5: 1, 6: 1, 10: 1}Out of the above two algorithms, The solution which has been implemented using for loop will have almost the same efficiency for each type of input list. Whereas, the solution which uses exception handling will execute much faster for input lists having only a few elements repeated over many times in the list. |