Python | Count unmatched elements
Last Updated :
10 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 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
OutputThe 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.
OutputThe 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.
OutputThe original list is : [3, 5, 1, 6, 7, 9]
The number of non-odd elements: 1
Time complexity: O(n)
Auxiliary Space: O(n)
Similar Reads
Python | Matching elements count Sometimes, while working with lists we need to handle two lists and search for the matches, and return just the count of indices of the match. Querying whole list for the this process is not feasible when the size of master list is very large, hence having just the match indices helps in this cause.
5 min read
Python - Test Common Elements Order Sometimes, while working with lists, we can have a problem in which we need to test if list contains common elements. This type of problem has been dealt many times and has lot of solutions. But sometimes, we might have a problem in which we need to check that those common elements appear in same or
4 min read
Python Tuple count() Method In this article, we will learn about the count() method used for tuples in Python. The count() method of a Tuple returns the number of times the given element appears in the tuple. Example Python3 tuple = (1, 2, 3, 1, 2, 3, 1, 2, 3) print(tuple.count(3)) Output : 3Python Tuple count() Method Syntax
3 min read
Python | Test list element similarity Given a list, your task is to determine the list is K percent same i.e has element that is populated more than K % times. Given below are few methods to solve the task. Method #1 Using collections.Counter Python3 # Python3 code to demonstrate # to check whether the list # K percent same or not from
4 min read
Python | Count of Matching i, j index elements Sometimes, while programming, we can have a problem in which we need to check for ith and jth character of each string. We may require to extract count of all strings with similar ith and jth characters. Letâs discuss certain ways in which this task can be performed. Method #1 : Using loop This is b
4 min read
Python - Unique values count of each Key Given a Dictionaries list, the task is to write a Python program to count the unique values of each key. Example: Input : test_list = [{"gfg" : 1, "is" : 3, "best": 2}, {"gfg" : 1, "is" : 3, "best" : 6}, {"gfg" : 7, "is" : 3, "best" : 10}] Output : {'gfg': 2, 'is': 1, 'best': 3} Explanation : gfg ha
7 min read