Python | Check if all elements in list follow a condition
Last Updated :
12 Apr, 2023
Sometimes, while working with Python list, we can have a problem in which we need to check if all the elements in list abide to a particular condition. This can have application in filtering in web development domain. Let's discuss certain ways in which this task can be performed.
Method #1 : Using all() We can use all(), to perform this particular task. In this, we feed the condition and the validation with all the elements is checked by all() internally.
Python3
# Python3 code to demonstrate working of
# Check if all elements in list follow a condition
# Using all()
# initializing list
test_list = [4, 5, 8, 9, 10]
# printing list
print("The original list : " + str(test_list))
# Check if all elements in list follow a condition
# Using all()
res = all(ele > 3 for ele in test_list)
# Printing result
print("Are all elements greater than 3 ? : " + str(res))
OutputThe original list : [4, 5, 8, 9, 10]
Are all elements greater than 3 ? : True
Time Complexity: O(n)
Auxiliary Space: O(n), where n is length of list.
Method #2 : Using itertools.takewhile() This function can also be used to code solution of this problem. In this, we just need to process the loop till a condition is met and increment the counter. If it matches list length, then all elements meet that condition.
Python3
# Python3 code to demonstrate working of
# Check if all elements in list follow a condition
# Using itertools.takewhile()
import itertools
# initializing list
test_list = [4, 5, 8, 9, 10]
# printing list
print("The original list : " + str(test_list))
# Check if all elements in list follow a condition
# Using itertools.takewhile()
count = 0
for ele in itertools.takewhile(lambda x: x > 3, test_list):
count = count + 1
res = count == len(test_list)
# Printing result
print("Are all elements greater than 3 ? : " + str(res))
OutputThe original list : [4, 5, 8, 9, 10]
Are all elements greater than 3 ? : True
Time Complexity: O(n) where n is the number of elements in the list “test_list”. itertools.takewhile() performs n number of operations.
Auxiliary Space: O(1), constant extra space is required
Method #3:Using lambda functions
Python3
# Python3 code to demonstrate working of
# Check if all elements in list follow a condition
# Using lambda functions
# initializing list
test_list = [4, 5, 8, 9, 10]
# printing list
print("The original list : " + str(test_list))
# Check if all elements in list follow a condition
# Using filter
res = list(filter(lambda x: x > 3, test_list))
if(len(res) == len(test_list)):
res = True
else:
res = False
# Printing result
print("Are all elements greater than 3 ? : " + str(res))
OutputThe original list : [4, 5, 8, 9, 10]
Are all elements greater than 3 ? : True
Time Complexity: O(N)
Auxiliary Space: O(N)
Method #4: Using map() and any()
Explanation: Using map() function, we can apply the given condition to each element in the list and return a list of True or False. The any() function will check if any of the element in the returned list is False, which means that not all elements in the original list follow the condition.
Python3
# initializing list
test_list = [4, 5, 8, 9, 10]
# printing list
print("The original list : " + str(test_list))
# Check if all elements in list follow a condition
# Using map() and any()
result = not any(map(lambda x: x <= 3, test_list))
# Printing result
print("Are all elements greater than 3 ? : " + str(result))
#This code is contributed by Edula Vinay Kumar Reddy
OutputThe original list : [4, 5, 8, 9, 10]
Are all elements greater than 3 ? : True
Time Complexity: O(N)
Auxiliary Space: O(N)
Method 5: Using a for loop to iterate over the list and check if each element is greater than 3.
- Initialize a boolean variable result to True.
- Iterate over each element x in the list test_list.
- Check if x is less than or equal to 3, if yes, set result to False and break the loop.
- Print the result.
Python3
# initializing list
test_list = [4, 5, 8, 9, 10]
# printing list
print("The original list : " + str(test_list))
# Check if all elements in list follow a condition
# Using for loop
result = True
for x in test_list:
if x <= 3:
result = False
break
# Printing result
print("Are all elements greater than 3 ? : " + str(result))
OutputThe original list : [4, 5, 8, 9, 10]
Are all elements greater than 3 ? : True
Time complexity: O(n) as it iterates over each element of the list once. :
Auxiliary space: O(1) as it only uses a boolean variable and a loop variable
Similar Reads
Check if any element in list satisfies a condition-Python
The task of checking if any element in a list satisfies a condition involves iterating through the list and returning True if at least one element meets the condition otherwise, it returns False. For example, in a = [4, 5, 8, 9, 10, 17], checking ele > 10 returns True as 17 satisfies the conditio
2 min read
Python - Check if List contains elements in Range
Checking if a list contains elements within a specific range is a common problem. In this article, we will various approaches to test if elements of a list fall within a given range in Python. Let's start with a simple method to Test whether a list contains elements in a range.Using any() Function -
3 min read
Python - Check List elements from Dictionary List
Sometimes, while working with data, we can have a problem in which we need to check for list element presence as a particular key in list of records. This kind of problem can occur in domains in which data are involved like web development and Machine Learning. Lets discuss certain ways in which thi
4 min read
Python | Check if all elements in a list are identical
Given a list, write a Python program to check if all the elements in that list are identical using Python. Examples: Input : ['a', 'b', 'c'] Output : False Input : [1, 1, 1, 1] Output : TrueCheck if all elements in a list are identical or not using a loop Start a Python for loop and check if the f
5 min read
Remove Elements From a List Based on Condition in Python
In Python, lists are a versatile and widely used data structure. There are often situations where you need to remove elements from a list based on a specific condition. In this article, we will explore five simple and commonly used methods to achieve this task. Remove Elements From A List Based On A
3 min read
Python | Check if two lists have any element in common
Checking if two lists share any common elements is a frequent requirement in Python. It can be efficiently handled using different methods depending on the use case. In this article, we explore some simple and effective ways to perform this check.Using set IntersectionSet intersection uses Python's
3 min read
Python - Check if all elements in List are same
To check if all items in list are same, we have multiple methods available in Python. Using SetUsing set() is the best method to check if all items are same in list. Pythona = [3, 3, 3, 3] # Check if all elements are the same result = len(set(a)) == 1 print(result) OutputTrue Explanation:Converting
3 min read
Python Check if the List Contains Elements of another List
The task of checking if a list contains elements of another list in Python involves verifying whether all elements from one list are present in another list. For example, checking if ["a", "b"] exists within ["a", "b", "c", "d"] would return True, while checking ["x", "y"] would return False. Using
3 min read
Python - Check if elements index are equal for list elements
Given two lists and check list, test if for each element in check list, elements occur in similar index in 2 lists. Input : test_list1 = [2, 6, 9, 7, 8], test_list2 = [2, 7, 9, 4, 8], check_list = [9, 8, 7] Output : False Explanation : 7 is at 4th and 2nd place in both list, hence False. Input : tes
4 min read
Python - Check if Tuple contains only K elements
Sometimes, while working with Python tuples, we can have a problem in which we need to test if any tuple contains elements from set K elements. This kind of problem is quite common and have application in many domains such as web development and day-day programming. Let's discuss certain ways in whi
3 min read