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
# Python 3 code to demonstrate # to get count of elements matching condition # using sum() + generator expression # initializing list test_list = [ 3 , 5 , 1 , 6 , 7 , 9 ] # printing original list print ("The original list is : " + str (test_list)) # using sum() + generator expression # to get count of elements matching condition # checks for odd res = sum ( 1 for i in test_list if i % 2 ! = 0 ) # printing result 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
# Python 3 code to demonstrate # to get count of elements matching condition # using sum()+ map() # initializing list test_list = [ 3 , 5 , 1 , 6 , 7 , 9 ] # printing original list print ("The original list is : " + str (test_list)) # using sum()+ map() # to get count of elements matching condition # checks for odd res = sum ( map ( lambda i: i % 2 ! = 0 , test_list)) # printing result 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
# Python 3 code to demonstrate # to get count of elements matching condition # using reduce() + lambda # initializing list test_list = [ 3 , 5 , 1 , 6 , 7 , 9 ] # printing original list print ("The original list is : " + str (test_list)) # using reduce() + lambda # to get count of elements matching condition # checks for odd res = reduce ( lambda count, i: count + (i % 2 ! = 0 ), test_list, 0 ) # printing result 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
# Python 3 code to demonstrate # to get count of elements matching condition # initializing list test_list = [ 3 , 5 , 1 , 6 , 7 , 9 ] # printing original list print ( "The original list is : " + str (test_list)) res = len ( list ( filter ( lambda x: x % 2 ! = 0 , test_list))) # printing result print ( "The number of odd elements: " + str (res)) |
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 # initialize the list test_list = [ 3 , 5 , 1 , 6 , 7 , 9 ] # print the original list print ( "The original list is :" , test_list) # use the Counter class to count the number of elements matching the condition # the condition here is i % 2 != 0, which checks for odd numbers # the Counter class returns a dictionary with the counts of each element counts = Counter(i % 2 ! = 0 for i in test_list) # print the count of odd elements # the count of odd elements is stored as the value for the key True in the counts dictionary print ( "The number of odd elements:" , counts[ True ]) #This code is contributed by Edula Vinay Kumar Reddy |
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 # printing original list 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)) #This code is contributed by Jyothi pinjala. |
The original list is : [3, 5, 1, 6, 7, 9] The number of odd elements: 5
Time complexity: O(N)
Auxiliary space: O(1)