Python | Count occurrences of an element in a listGiven 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
Method 2 (Using count()) Python3
Method 2 (Using Counter()) Python3
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
Python list-programs python-list Practice Tags :
python-list 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”. 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) 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] Count = My_List.count(3) Print(“Occurrence of 4 in my list is:”, count) 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. 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.")
|