Below are the three solutions:Fastest is using a for loop and storing it in a Dict. Show 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 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. 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
Article Tags :
Python
Python list-programs python-list Practice Tags :
python-list Python List count()In this tutorial, we will learn about the Python List count() method with the help of examples. The count() method returns the number of times the specified element appears in the list. Example# create a list numbers = [2, 3, 5, 2, 11, 2, 7]# check the count of 2
count = numbers.count(2)
print('Count of 2:', count)
# Output: Count of 2: 3How to count the number of occurrences of a list item in PythonUse .count() to Count Number of Occurrences in a Python ListThe easiest way to count the number of occurrences in a Python list of a given item is to use the Python .count() method. The method is applied to a given list and takes a single argument. The argument passed into the method is counted and the number of occurrences of that item in the list is returned. Let’s see how we can use the .count() method to count the number of occurrences in a Python list: # Count the Number of Occurrences in a Python list using .count() items = ['a', 'b', 'a', 'c', 'd', 'd', 'd', 'c', 'a', 'b'] count_a = items.count('a') print(f'{count_a=}') # Returns: count_a=3We can see here that when we apply the .count() method to a list and pass in the item that we want to count, that the number of occurrences are returned. Let’s see what would happen if we pass in an item that does not exist in the list: # Count the Number of Occurrences in a Python list using .count() items = ['a', 'b', 'a', 'c', 'd', 'd', 'd', 'c', 'a', 'b'] count_f = items.count('f') print(f'{count_f=}') # Returns: count_f=0When an item doesn’t exist in a list and the .count() method is applied, the value of 0 is returned. This method is a very Pythonic way to get the number of occurrences in a Python list for a single item. However, if you wanted to count the number occurrences of more than one item in a list, it’d be much better to use a different method, such as the Counter library. This is exactly what you’ll learn in the next section! Need to automate renaming files? Check out this in-depth guide on using pathlib to rename files. More of a visual learner, the entire tutorial is also available as a video in the post! |