Python | Count unmatched elements
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 number that does not match the particular condition to have a distinguish which match for further utilization. Lets discuss certain ways in which this task can be achieved.
Method #1 : Using loop
This is brute force method to perform this particular task. In this, we iterate list, find elements that does not match a particular condition and take count.
Python3
test_list = [ 3 , 5 , 1 , 6 , 7 , 9 ]
print ( "The original list is : " + str (test_list))
res = 0
for ele in test_list:
if not ele % 2 ! = 0 :
res = res + 1
print ( "The number of non-odd elements: " + str (res))
|
Output :
The original list is : [3, 5, 1, 6, 7, 9]
The number of non-odd elements: 1
Time complexity: O(n), where n is the length of the test_list. The loop takes O(n) time
Auxiliary Space: O(1), extra space of size n is required
Method #2 : Using len() + generator expression
This method uses the trick of counting elements to the add 1 whenever the generator expression returns False. By the time list gets exhausted, count of numbers not matching a condition is returned.
Python3
test_list = [ 3 , 5 , 1 , 6 , 7 , 9 ]
print ( "The original list is : " + str (test_list))
res = len ( list (i for i in test_list if not i % 2 ! = 0 ))
print ( "The number of non-odd elements: " + str (res))
|
Output :
The original list is : [3, 5, 1, 6, 7, 9]
The number of non-odd elements: 1
Time Complexity: O(n), where n is the length of the input list. This is because we’re using len() + generator expression which has a time complexity of O(n) in the worst case.
Auxiliary Space: O(1), as we’re using constant additional space
Method #3 : Using reduce() + lambda
This method uses the reduce function to iterate through the list and add 1 to the count whenever the lambda function returns False. The reduce function accumulates the count and returns the final count of elements that do not match the condition.
Python3
from functools import reduce
test_list = [ 3 , 5 , 1 , 6 , 7 , 9 ]
print ( "The original list is : " + str (test_list))
res = reduce ( lambda x, y: x + 1 if not y % 2 ! = 0 else x, test_list, 0 )
print ( "The number of non-odd elements: " + str (res))
|
Output
The original list is : [3, 5, 1, 6, 7, 9]
The number of non-odd elements: 1
Time complexity: O(n)
Auxiliary Space: O(1)
Method #4 : Using a list comprehension:
Python3
test_list = [ 3 , 5 , 1 , 6 , 7 , 9 ]
print ( "The original list is : " + str (test_list))
res = len ([i for i in test_list if i % 2 = = 0 ])
print ( "Number of non-odd elements:" , res)
|
Output
The original list is : [3, 5, 1, 6, 7, 9]
Number of non-odd elements: 1
Time complexity: O(n)
Auxiliary Space: O(n)
Method#5: Using filter()
Python3
test_list = [ 3 , 5 , 1 , 6 , 7 , 9 ]
print ( "The original list is : " + str (test_list))
res = len ( list ( filter ( lambda ele: ele % 2 = = 0 , test_list)))
print ( "The number of non-odd elements: " + str (res))
|
Output
The original list is : [3, 5, 1, 6, 7, 9]
The number of non-odd elements: 1
Time complexity: O(n)
Auxiliary Space: O(n)
Last Updated :
10 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...