Python 3 – List count() Method
Lists are one of the most frequently used data structures in Python programming. Python lists are dynamic in nature and can hold different types of data, such as integers, floats, strings, etc. Python provides a range of inbuilt functions to manipulate and operate on lists effectively. One such function is the count()
method, which is used to count the occurrence of a given element in a list.
List count() Method
The count()
method is a list method in Python, which counts the number of times a specified element appears in the list. The method takes a single argument, which is the element to be counted. The count()
method returns the number of times the given element appears in the list.
Syntax
list.count(element)
Parameters:
– element
: The element whose occurrence is to be counted.
Return Value:
– The count() method returns the number of times the specified element appears in the list.
If the specified element is not present in the list, the method returns zero.
Sample Code
Let’s see some examples of how we can use the count()
method to count the occurrence of a given element in a list.
# Example 1
fruits = ["apple", "banana", "orange", "mango", "orange"]
count = fruits.count("orange")
print("The number of times 'orange' appears in the list: ", count)
# Example 2
numbers = [1, 2, 1, 3, 4, 5, 1, 6, 7, 8, 1]
count = numbers.count(1)
print("The number of times '1' appears in the list: ", count)
# Example 3
mixed_list = ["apple", 1, 2.0, "banana", "apple", 1, "mango", "banana"]
count = mixed_list.count("apple")
print("The number of times 'apple' appears in the list: ", count)
Output:
The number of times 'orange' appears in the list: 2
The number of times '1' appears in the list: 4
The number of times 'apple' appears in the list: 2
Python count() method with Tuples
The count()
method can also be used with tuples, another frequently used data structure in Python. A tuple is a collection of values that are ordered and immutable. Since tuples are immutable, their elements cannot be modified, added or removed.
Syntax
tuple.count(element)
Parameters:
– element
: The element whose occurrence is to be counted.
Return Value:
– The count() method returns the number of times the specified element appears in the tuple.
If the specified element is not present in the tuple, the method returns zero.
Sample Code
# Example 1
numbers = (1, 2, 1, 3, 4, 5, 1, 6, 7, 8, 1)
count = numbers.count(1)
print("The number of times '1' appears in the tuple: ", count)
# Example 2
fruits = ("apple", "banana", "orange", "mango", "orange")
count = fruits.count("orange")
print("The number of times 'orange' appears in the tuple: ", count)
Output:
The number of times '1' appears in the tuple: 4
The number of times 'orange' appears in the tuple: 2
Python count() method with Sets
The count()
method cannot be used with sets in Python because sets are unordered and do not support indexing.
Conclusion
The count()
method is a useful inbuilt function in Python that can be used to count the occurrence of a given element in a list or tuple. It is a simple and efficient way to obtain the number of times an element appears in a Python list or tuple without writing a loop.