In this tutorial, we will look at how to check if all the elements in a list are True or not in Python with the help of some examples.
How to check if all the list values are True?
You can use the Python built-in all()
function to check if all the elements in a list are True or not.
The all()
function takes in an iterable as an argument and returns True
if all the values in the iterable are truthy (represent True
in a boolean context).
Now, if you want to check whether all the values in a list evaluate to True in a boolean context, directly pass the list as an argument to the all()
function.
# check if all the list values are truthy all(ls)
If, on the other hand, you want to check whether each value in the list is the boolean value True
, then inside the all()
function, use an iterator to check whether each list value is True
or not.
# check if all the list values are True all(val == True for val in ls)
It returns True
if all the values in the list are True.
Examples
Let’s now look at some examples of using the above methods. First, we will create a few lists that we’ll use to demonstrate the methods.
# list with all True values ls1 = [True, True, True, True] # list with not all values True ls2 = [False, True, False, True] # empty list ls3 = [] # display the lists print(ls1) print(ls2) print(ls3)
Output:
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
[True, True, True, True] [False, True, False, True] []
Here, we created three lists – ls1
, ls2
, and ls3
. The list ls1
contains only True
as its elements. The list ls2
has repeated values but not all values are True
and the list ls3
is empty (it does not contain any elements).
Example 1 – Check if all the list elements are True using all()
As mentioned earlier, the all()
function returns True
if all the values in the iterable are truthy (evaluate to True
in a boolean context).
Let’s now apply the all()
function to the lists created above.
# check if all values in list evaluate to True print(all(ls1)) print(all(ls2)) print(all(ls3))
Output:
True False True
We get True
for ls1
and False
for ls2
. Note that if you apply the all()
function to an empty list, you get True
as the result.
Example 2 – Using all()
on list with non-boolean values
Note that the all()
does not exactly check for whether the individual values are True or False. Instead, it checks whether the individual values evaluate to True
or False
in a boolean context.
For example, the value 1
evaluates to True
in a boolean context. Similarly, the values 0
and None
, empty collections (such as lists), etc. evaluate to False
in a boolean context. Non-empty collections, numeric values other than 0
, etc. evaluate to True
in a boolean context.
Let’s look at an example.
# list with truthy values ls4 = [1, 3, 1, True] print(all(ls4))
Output:
True
Here, we applied the all()
function to a list containing truthy values (all the values result to True
in a boolean context) and thus we get True
as the output.
If, the above list has any falsy (evaluate to False
in a boolean context) values, the all()
function will result in False
.
ls4 = [0, 3, 1, True] print(all(ls4))
Output:
False
We get False
as the result.
Now, depending on your use case, you might only want to check whether the values in a list are truthy or not. In that case, use the all()
function directly and pass the list (or the iterable) as an argument.
If, on the other hand, you want to check whether all the values in a list are exactly equal to the boolean value True
, then, inside the all()
function, check if each value is exactly equal to True
or not using an iterator.
Here’s an example.
# list with truthy values ls4 = [1, 3, 1, True] # check if all values are exactly True print(all(val == True for val in ls4))
Output:
False
We get False
as the output even if all the values in the above list are truthy.
Summary
In this tutorial, we looked at how to check if all the values in a list are True or not. The following are the key takeaways –
- Use the Python built-in
all()
function to check if all the elements in a list are truthy or not. - In case you want to check whether each value is exactly
True
or not, iterate through each value and check if it’s equal toTrue
using an iterator inside theall()
function.
You might also be interested in –
- Python – Check If All Elements in List are Negative
- Python – Check If All Elements in List are Positive
- Python – Check If All Elements in List are Strings
- Python – Check If All Elements in List are Integers
- Python – Check If All Elements in List are None
- Python – Check If All Elements in List are Zero
- Python – Check If All Elements in a List are Equal
- Python – Check If All Elements in a List are Unique
- Check If a List Contains Only Numbers in Python
- Python – Check List Contains All Elements Of Another List
- Python – Check if an element is in a list
- Python – Check If List Is Sorted Or Not
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.