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: 3
Syntax of List count[]
The syntax of the count[]
method is:
list.count[element]
count[] Parameters
The count[]
method takes a single
argument:
- element - the element to be counted
Return value from count[]
The count[]
method returns the number of times element appears in the list.
Example 1: Use of count[]
# vowels list
vowels = ['a', 'e', 'i', 'o', 'i', 'u']
# count element 'i'
count = vowels.count['i']
# print count
print['The count of i is:', count]
# count element 'p'
count = vowels.count['p']
# print count
print['The count of p is:', count]
Output
The count of i is: 2 The count of p is: 0
Example 2: Count Tuple and List Elements Inside List
# random list
random = ['a', ['a', 'b'], ['a', 'b'], [3, 4]]
# count element ['a', 'b']
count = random.count[['a', 'b']]
# print count
print["The count of ['a', 'b'] is:", count]
# count element [3, 4]
count = random.count[[3, 4]]
# print count
print["The count of [3, 4] is:", count]
Output
The count of ['a', 'b'] is: 2 The count of [3, 4] is: 1
The count[] is a built-in function in Python. It will return the total count of a given element in a list. The count[] function is used to count elements on a list as well as a string.
In this Python tutorial, you will learn:
- Python count
- Python List count[]
- Example 1: List Count
- Example 2: Find the count of elements [Duplicates] in a givenlist
Python List count[]
The count[] is a built-in function in Python. It will return you the count of a given element in the list.
Syntax:
list.count[element]
Parameters:
element: The element you want to find the count.
ReturnValue:
The count[] method will return an integer value, i.e., the count of the given element from the given list. It returns a 0 if the value is not found in the given list.
Example 1: List Count
Following example shows the working of count[] function on a list:
list1 = ['red', 'green', 'blue', 'orange', 'green', 'gray', 'green'] color_count = list1.count['green'] print['The count of color: green is ', color_count]
Output:
The count of color: green is 3
Example 2: Find the count of elements [Duplicates] in a givenlist
list1 = [2,3,4,3,10,3,5,6,3] elm_count = list1.count[3] print['The count of element: 3 is ', elm_count]
Output:
The count of element: 3 is 4
Summary:
- The count[] is a built-in function in Python. It will return you the count of a given element in a list or a string.
- In the case of a list, the element to be counted needs to be given to the count[] function, and it will return the count of the element.
- The count[] method returns an integer value.
Below are the three solutions:
Fastest is using a for loop and storing it in a Dict.
import time
from collections import Counter
def countElement[a]:
g = {}
for i in a:
if i in g:
g[i] +=1
else:
g[i] =1
return g
z = [1,1,1,1,2,2,2,2,3,3,4,5,5,234,23,3,12,3,123,12,31,23,13,2,4,23,42,42,34,234,23,42,34,23,423,42,34,23,423,4,234,23,42,34,23,4,23,423,4,23,4]
#Solution 1 - Faster
st = time.monotonic[]
for i in range[1000000]:
b = countElement[z]
et = time.monotonic[]
print[b]
print['Simple for loop and storing it in dict - Duration: {}'.format[et - st]]
#Solution 2 - Fast
st = time.monotonic[]
for i in range[1000000]:
a = Counter[z]
et = time.monotonic[]
print [a]
print['Using collections.Counter - Duration: {}'.format[et - st]]
#Solution 3 - Slow
st = time.monotonic[]
for i in range[1000000]:
g = dict[[[i, z.count[i]] for i in set[z]]]
et = time.monotonic[]
print[g]
print['Using list comprehension - Duration: {}'.format[et - st]]
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 - Fast
Counter[{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