Friday, October 10, 2025
HomeLanguagesPython | Count unmatched elements

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 = 0
for ele in test_list:
    if not ele % 2 != 0:
        res = res + 1
           
# printing result
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




# 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))


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




# Python 3 code to demonstrate
# Count unmatched elements
# using reduce() + lambda
 
from functools import reduce
 
# initializing list
test_list = [3, 5, 1, 6, 7, 9]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# using reduce() + lambda
# Count unmatched elements
# checks for odd
res = reduce(lambda x, y: x + 1 if not y % 2 != 0 else x, test_list, 0)
 
# printing result
print ("The number of non-odd elements: " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy


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]
# printing original list
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)
#This code is contributed by Jyothi pinjala.


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




# 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.


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)

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32350 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6718 POSTS0 COMMENTS
Nicole Veronica
11880 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6838 POSTS0 COMMENTS
Ted Musemwa
7099 POSTS0 COMMENTS
Thapelo Manthata
6794 POSTS0 COMMENTS
Umr Jansen
6794 POSTS0 COMMENTS