Get the number of specific elements in a python list

Count how many specific elements are in the list

You can use count () when you want to count how many elements in a list are. It can also be used for strings.

count()

l = [1, 2, 2, 3, 3, 3]
print(l.count(2)) 

#Output result: 2
s = 'hello world!'
print(s.count('o'))

#Output result: 2

Other

l = [1, 2, 2, 3, 3, 3]
print(len([a for a in l if a == 2]))  # 2
l = [1, 2, 2, 3, 3, 3]
print(sum([a == 2 for a in l]))  # 2

You can count the elements that meet the conditions here.

Recommended Posts

Get the number of specific elements in a python list
Get the size (number of elements) of UnionFind in Python
Get the value of a specific key in a list from the dictionary type in the list with Python
Get the caller of a function in Python
Make a copy of the list in Python
Get only the subclass elements in a list
Get the number of readers of a treatise on Mendeley in Python
Get the value of a specific key up to the specified index in the dictionary list in Python
How to get the number of digits in Python
[python] Get the list of classes defined in the module
[Python] Outputs all combinations of elements in the list
Group by consecutive elements of a list in Python
How to get a list of files in the same directory with python
How to get the last (last) value in a list in Python
How to get a list of built-in exceptions in python
Get the number of occurrences for each element in the list
Display a list of alphabets in Python 3
[python] Get a list of instance variables
Get the EDINET code list in Python
[Python] Get a list of folders only
How to identify the element with the smallest number of characters in a Python list?
How to check in Python if one of the elements of a list is in another list
Try to get a list of breaking news threads in Python.
[Python] Let's reduce the number of elements in the result in set operations
[python] Get the rank of the values in List in ascending / descending order
Get a list of files in a folder with python without a path
Recursively get the Excel list in a specific folder with python and write it to Excel.
[python] Check the elements of the list all, any
[Python] Get the files in a folder with Python
[Python] Sort the list of pathlib.Path in natural sort
Find the number of days in a month
Rewriting elements in a loop of lists (Python)
Output in the form of a python array
Get a glimpse of machine learning in Python
Sort list elements in a specified order in Python
[Python] Manipulating elements in a list (array) [Sort]
Get a capture of the entire web page in Selenium Python VBA
Get the number of searches with a regular expression. SeleniumBasic VBA Python
Generate a list packed with the number of days in the current month.
Check the in-memory bytes of a floating point number float in Python
[Linux] Command to get a list of commands executed in the past
Receive a list of the results of parallel processing in Python with starmap
Get a datetime instance at any time of the day in Python
Get the number of digits
Getting list elements in Python
Check if the string is a number in python
[Python] A program that counts the number of valleys
[Python] Manipulation of elements in list (array) [Add / Delete]
[Python] Get the list of ExifTags names of Pillow library
Python: Get a list of methods for an object
[Python] Get the number of views of all posted articles
Get the URL of the HTTP redirect destination in Python
A reminder about the implementation of recommendations in Python
How to pass the execution result of a shell command in a list in Python
plot the coordinates of the processing (python) list and specify the number of times in draw ()
Set the number of elements in a NumPy one-dimensional array to a power of 2 (0 padded)
Python script to get a list of input examples for the AtCoder contest
Extract elements (using a list of indexes) in a NumPy style from a Python list / tuple
Make sure all the elements in the list are the same in Python
Try to get the function list of Python> os package
Find out the apparent width of a string in python