Python | Check if all elements in list follow a condition
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 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)) |
The original list : [4, 5, 8, 9, 10] Are all elements greater than 3 ? : True
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 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 = 0for ele in itertools.takewhile(lambda x: x > 3, test_list): count = count + 1res = count == len(test_list) # Printing result print("Are all elements greater than 3 ? : " + str(res)) |
The original list : [4, 5, 8, 9, 10] Are all elements greater than 3 ? : True
Recommended Posts:
- Python | Check if any element in list satisfies a condition
- Python | Check whether two lists follow same pattern or not
- Python | Check if all elements in a List are same
- Python | Check if list contains all unique elements
- Python | Check if all elements in a list are identical
- Check if elements of an array can be arranged satisfying the given condition
- Python | Count of elements matching particular condition
- Python | Remove first K elements matching some condition
- Python | Test if any list element returns true for condition
- Python | Check if given string can be formed by concatenating string elements of list
- Python | Sorting list of lists with similar list elements
- Python | Merge List with common elements in a List of Lists
- Python | Subtract two list elements if element in first list is greater
- Python | Add list elements with a multi-list based on index
- Python | Replace elements in second list with index of same element in first list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

