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
# Python 3 code to demonstrate # Count unmatched elements # using loop # initializing list test_list = [3, 5, 1, 6, 7, 9] # printing original list print ("The original list is : " + str(test_list)) # using loop # Count unmatched elements # checks for odd res = 0for ele in test_list: if not ele % 2 != 0: res = res + 1 # printing result print ("The number of non-odd elements: " + str(res)) |
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
# Python 3 code to demonstrate # Count unmatched elements # using len() + generator expression # initializing list test_list = [3, 5, 1, 6, 7, 9] # printing original list print ("The original list is : " + str(test_list)) # using len() + generator expression # Count unmatched elements # checks for odd res = len(list(i for i in test_list if not i % 2 != 0)) # printing result print ("The number of non-odd elements: " + str(res)) |
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
# Python 3 code to demonstrate # Count unmatched elements# using reduce() + lambdafrom functools import reduce# initializing listtest_list = [3, 5, 1, 6, 7, 9]# printing original listprint ("The original list is : " + str(test_list))# using reduce() + lambda# Count unmatched elements# checks for oddres = reduce(lambda x, y: x + 1 if not y % 2 != 0 else x, test_list, 0)# printing resultprint ("The number of non-odd elements: " + str(res))#This code is contributed by Edula Vinay Kumar Reddy |
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]# printing original listprint ("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)#This code is contributed by Jyothi pinjala. |
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
# initializing list test_list = [3, 5, 1, 6, 7, 9] # printing original list print ("The original list is : " + str(test_list)) # using filter function # Count unmatched elements # checks for odd res = len(list(filter(lambda ele: ele % 2 == 0, test_list)))# printing result print ("The number of non-odd elements: " + str(res))#This code is contributed by Vinay Pinjala. |
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)



