Python | Count occurrences of an element in a list
Given a list in Python and a number x, count number of occurrences of x in the given list. Show
Recommended: Please try your approach on {IDE} first, before moving on to the solution. Method 1 (Simple approach) Python3
Output: 8 has occurred 5 times Method 2 (Using count()) Python3
Output: 8 has occurred 5 times Method 2 (Using Counter()) Python3
Output: 3 has occurred 2 times Article Tags : Python Python list-programs python-list Practice Tags : python-list Below are the three solutions:Fastest is using a for loop and storing it in a Dict. import time from collections import Counter def countElement(a): g = {} for i in a: if i in g: g[i] +=1 else: g[i] =1 return g z = [1,1,1,1,2,2,2,2,3,3,4,5,5,234,23,3,12,3,123,12,31,23,13,2,4,23,42,42,34,234,23,42,34,23,423,42,34,23,423,4,234,23,42,34,23,4,23,423,4,23,4] #Solution 1 - Faster st = time.monotonic() for i in range(1000000): b = countElement(z) et = time.monotonic() print(b) print('Simple for loop and storing it in dict - Duration: {}'.format(et - st)) #Solution 2 - Fast st = time.monotonic() for i in range(1000000): a = Counter(z) et = time.monotonic() print (a) print('Using collections.Counter - Duration: {}'.format(et - st)) #Solution 3 - Slow st = time.monotonic() for i in range(1000000): g = dict([(i, z.count(i)) for i in set(z)]) et = time.monotonic() print(g) print('Using list comprehension - Duration: {}'.format(et - st))Result #Solution 1 - Faster{1: 4, 2: 5, 3: 4, 4: 6, 5: 2, 234: 3, 23: 10, 12: 2, 123: 1, 31: 1, 13: 1, 42: 5, 34: 4, 423: 3} Simple for loop and storing it in dict - Duration: 12.032000000000153 #Solution 2 - FastCounter({23: 10, 4: 6, 2: 5, 42: 5, 1: 4, 3: 4, 34: 4, 234: 3, 423: 3, 5: 2, 12: 2, 123: 1, 31: 1, 13: 1}) Using collections.Counter - Duration: 15.889999999999418 #Solution 3 - Slow{1: 4, 2: 5, 3: 4, 4: 6, 5: 2, 34: 4, 423: 3, 234: 3, 42: 5, 12: 2, 13: 1, 23: 10, 123: 1, 31: 1} Using list comprehension - Duration: 33.0 Count Occurrences of Element in Python ListPython is well known for its easy syntax, fast implementation, and, most importantly, large support of multiple data structures. Lists are one of those data structures in python which helps to store large amounts of sequential data in a single variable. As huge data is stored under the same variable, it is sometimes quite difficult to manually identify whether the given element is present in the lists, and if yes, how many times. Therefore, in this article, we will study the various ways to count the number of occurrences in the list in python. To recall the concepts of python lists in detail, visit our article “3 Ways to Convert List to Tuple”. Example 1:Here we discussed the most efficient way to get the total count of occurrences of items in the list by using the list count() method. The function count() takes a value as an input, and after calculating the occurrences of that element, it returns an integer value. To write a program for the count() function, we use the Spyder compiler so that you can understand how it calculates the occurrence of a number. We first create a new blank file and then start writing python code. Initially, we create a python list and add values to it. Then we use a list count and print function that calculates an element’s number of occurrences and then displays its result in the form of an integer value. My_list = [ 3, 4, 8, 4, 0, 6, 1, 3, 11, 17, 11, 4] We save our source file and specify its name “PythonCountOccurrences.” After this, run the file and check count() calculates the existence of an element in the list. Python Program to Count occurrences of an element in a listIn this tutorial, you will learn to count occurrences of an element in a list in Python. The List is an ordered set of values enclosed in square brackets [ ]. List stores some values called elements in it, which can be accessed by their particular index. Occurrence simply means the total number of times an element is present in the list. An element in a list can be repeated many times in the list. We have to write a program that will input an element from the user and then count and display the number of times that element is repeated in the list. Input: list=[2, 4, 1, 7, 9, 10, 12, 17, 1] n=1 Output: 2 ( 1 occurs 2 times in the list) Input: list=[3, 4, 5, 7, 8, 10, 2] n=1 Output: 0 (1 occurs 0 times in the list) methods to count occurences
Using built-in count() function to count occurrences in a listThe python has a built-in count() function which takes a value as an argument whose count you want to find in the list. It will return an integer value that represents the number of times a specified item occurs in the list. If the integer does not exist, it returns 0. following is the code To count occurences of an element in a list in python# Create a list of integers list1 = [1, 2, 2, 7, 2, 9, 1, 4, 3] x = list1.count(2) print("The number 2 occurs " + str(x) + " times in the list.") Output: The number 2 occurs 3 times in the list.# Create a list of integers list1 = [1, 2, 2, 7, 2, 9, 1, 4, 3] x = list1.count(2) print("The number 2 occurs " + str(x) + " times in the list.") Output: The number 2 occurs 3 times in the list. Example 2:Similarly, if you apply the same method to determine the same elements in strings. # Create a list of Strings list1 = ['mango', 'mango', 'guava', 'apple', 'guava', 'mango', 'apple'] x = list1.count('apple') print("The Fruit 'apple' occurs " + str(x) + " times in the list.") x = list1.count('orange') print("The Fruit 'orange' occurs " + str(x) + " times in the list.") |