python
Get count of a specific element in a list using Python
We can use the count() function of the Python list to get the count of a specific element in the list.
# Define a list
numbers = [4, 4, 5, 9, 4, 3, 6, 9]
# Get the frequency of 4
result = numbers.count(4)
# Print the result
print(result)
Output
3
In the code snippet
- We have created a list called numbers that contains multiple items.
- We are using the count() function of the list and getting the frequency of 4 in the numbers list and assigning them to the result named variable.
- Printing the result.
List.count() function
As we have already explained the functionality of List.count() function in Python. The count() function returns the count of a specific item in the list.
Syntax
List.count(item)
Code Example
my_list = ['a', 'b', 'a', 'c', 'd', 'a', 'a']
result = my_list.count('a')
print(result)
Output
4
Was this helpful?
Similar Posts
- Remove items less than a specific value from Python list
- Add item at a specific position in a Python List
- Get the count of rows and columns of a Pandas DataFrame
- Count Rows group by column name SqlAlchemy
- Count NaN values in a Pandas DataFrame
- Replace DataFrame column values with a specific value
- remove rows base on NaN of specific column