Python | Count of elements matching particular condition
Last Updated :
12 Apr, 2023
Checking a number/element by a condition is a common problem one faces and is done in almost every program. Sometimes we also require to get the totals that match the particular condition to have a distinguish which to not match for further utilization. Lets discuss certain ways in which this task can be achieved.
Method #1 : Using sum() + generator expression This method uses the trick of adding 1 to the sum whenever the generator expression returns true. By the time list gets exhausted, summation of count of numbers matching a condition is returned.
Python3
test_list = [ 3 , 5 , 1 , 6 , 7 , 9 ]
print ("The original list is : " + str (test_list))
res = sum ( 1 for i in test_list if i % 2 ! = 0 )
print ("The number of odd elements: " + str (res))
|
Output :
The original list is : [3, 5, 1, 6, 7, 9]
The number of odd elements: 5
Method #2 : Using sum() + map() map() does the task almost similar to the generator expression, difference is just the internal data structure employed by it is different hence more efficient.
Python3
test_list = [ 3 , 5 , 1 , 6 , 7 , 9 ]
print ("The original list is : " + str (test_list))
res = sum ( map ( lambda i: i % 2 ! = 0 , test_list))
print ("The number of odd elements: " + str (res))
|
Output :
The original list is : [3, 5, 1, 6, 7, 9]
The number of odd elements: 5
Time Complexity: O(n), where n is length of test_list.
Auxiliary Space: O(1)
Method #3 : Using reduce() + lambda reduce function does the task of accumulation as the sum function in the above used methods. lambda is used to perform the condition against which result needs to be checked.
Python3
test_list = [ 3 , 5 , 1 , 6 , 7 , 9 ]
print ("The original list is : " + str (test_list))
res = reduce ( lambda count, i: count + (i % 2 ! = 0 ), test_list, 0 )
print ("The number of odd elements: " + str (res))
|
Output :
The original list is : [3, 5, 1, 6, 7, 9]
The number of odd elements: 5
Method #4: Using filter()+len()+list()+lambda functions
Python3
test_list = [ 3 , 5 , 1 , 6 , 7 , 9 ]
print ( "The original list is : " + str (test_list))
res = len ( list ( filter ( lambda x: x % 2 ! = 0 , test_list)))
print ( "The number of odd elements: " + str (res))
|
Output
The original list is : [3, 5, 1, 6, 7, 9]
The number of odd elements: 5
Time Complexity: O(N)
Auxiliary Space: O(N)
Method #5 : Using the collections.Counter class:
Use the Counter class to count the number of elements matching the condition
Python3
from collections import Counter
test_list = [ 3 , 5 , 1 , 6 , 7 , 9 ]
print ( "The original list is :" , test_list)
counts = Counter(i % 2 ! = 0 for i in test_list)
print ( "The number of odd elements:" , counts[ True ])
|
Output
The original list is : [3, 5, 1, 6, 7, 9]
The number of odd elements: 5
Time complexity: O(N)
Auxiliary space: O(N)
Method #6 : Using a for loop
Python3
test_list = [ 3 , 5 , 1 , 6 , 7 , 9 ]
res = 0
print ( "The original list is : " + str (test_list))
for i in test_list:
if i % 2 ! = 0 :
res + = 1
print ( "The number of odd elements: " + str (res))
|
Output
The original list is : [3, 5, 1, 6, 7, 9]
The number of odd elements: 5
Time complexity: O(N)
Auxiliary space: O(1)
Share your thoughts in the comments
Please Login to comment...