I often use the Python all() function in automation scripts that validate data before producing a report. For example, before a sales-report script emails a CSV file to a manager, I need to confirm that every required field exists, every amount is valid, and every employee record meets the rules.
You could write several for loops and flags for those checks. But when every condition must pass, all() gives you a short, readable answer: True only when every item is truthy.
This practical Python tutorial shows how the all() function in Python works, where it fits best, and how to use it safely in real scripts.
What Is the Python all() Function?
The Python all() function is a built-in function that returns True when every item in an iterable is truthy. An iterable is an object you can loop through, such as a list, tuple, set, dictionary, generator, or string.
Here is the basic syntax:
all(iterable)
The function returns:
Truewhen every item is truthyFalsewhen at least one item is falsyTruewhen the iterable is empty
In Python, common falsy values include:
False00.0""(an empty string)[](an empty list){}(an empty dictionary)None
Almost every other value counts as truthy.
Here is the simplest example:
daily_sales = [1250, 980, 1675, 720]
result = all(daily_sales)
print(result)
Output:
True
I executed the above example code and added the screenshot below.

Every number in daily_sales is non-zero, so Python treats each value as truthy.
Now add a zero:
daily_sales = [1250, 980, 0, 720]
result = all(daily_sales)
print(result)
Output:
False
The 0 makes the result False.
The important point is that all() does not automatically check business rules such as “sales must be greater than 500.” It checks whether values are truthy unless you provide a comparison expression.
If you are new to Python built-in functions, also explore this guide on Python built-in functions. Knowing these functions helps you write smaller and cleaner scripts.
Python all() Function Syntax and Rules
The all() function in Python accepts one argument: an iterable. You can pass a list, tuple, set, dictionary, generator expression, or another iterable object.
all(iterable)
Here is a quick reference table:
| Input | Result | Why |
|---|---|---|
[True, True, True] | True | Every value is truthy |
[True, False, True] | False | One value is falsy |
[1, 2, 3] | True | Non-zero integers are truthy |
[1, 0, 3] | False | Zero is falsy |
["James", "Olivia"] | True | Non-empty strings are truthy |
["James", ""] | False | Empty string is falsy |
[] | True | No item fails the check |
The empty iterable result surprises many beginners:
validation_results = []
print(all(validation_results))
Output:
True
Python returns True because there is no False item in the list. This behavior follows a logical rule: “all items pass” is true when no item exists that fails.
In reporting scripts, this can cause an issue if an empty list means “no records were loaded.” Later in this article, I will show how to handle that case correctly.
Pro Tip: I’ve found that
all([])causes subtle validation bugs when a script receives no data. When an empty list should fail validation, always check that the list contains records before callingall().
Use Python all() Function With Boolean Values
The clearest way to learn all() is with a list of Boolean values.
Imagine a small report-export script. Before it saves the report, it checks whether the source file exists, the employee data loaded correctly, and the output folder is available.
source_file_exists = True
employee_data_loaded = True
output_folder_exists = True
can_create_report = all([
source_file_exists,
employee_data_loaded,
output_folder_exists
])
print(can_create_report)
Output:
True
I executed the above example code and added the screenshot below.

Each variable holds either True or False. Since every check passes, the script can continue.
Now assume the employee data failed to load:
source_file_exists = True
employee_data_loaded = False
output_folder_exists = True
can_create_report = all([
source_file_exists,
employee_data_loaded,
output_folder_exists
])
print(can_create_report)
Output:
False
This pattern works well when you already have several validation results. It keeps the final decision easy to read.
You can place the condition directly inside an if statement:
source_file_exists = True
employee_data_loaded = True
output_folder_exists = False
if all([source_file_exists, employee_data_loaded, output_folder_exists]):
print("Creating the monthly sales report.")
else:
print("Cannot create the report. Check the input file, data, and output folder.")
Output:
Cannot create the report. Check the input file, data, and output folder.
I executed the above example code and added the screenshot below.

Use descriptive variable names. A name such as employee_data_loaded makes the code easier to review than a vague name such as check1.
For more help creating readable names, see Python naming conventions for variables.
Use Python all() Function With Lists
The most common use of the Python all() function is validating every value in a list.
Check Whether Every Number Is Positive
Suppose Amanda in Denver sends a list of weekly order totals. Your script should continue only if every value is greater than zero.
weekly_order_totals = [425, 780, 610, 950, 320]
all_positive = all(total > 0 for total in weekly_order_totals)
print(all_positive)
Output:
True
The expression:
total > 0 for total in weekly_order_totals
creates Boolean results one at a time. Python checks these values through all().
The logical result looks like this:
[True, True, True, True, True]
Now include an invalid total:
weekly_order_totals = [425, 780, 0, 950, 320]
all_positive = all(total > 0 for total in weekly_order_totals)
print(all_positive)
Output:
False
The 0 fails the total > 0 rule.
A generator expression is useful here because it avoids creating a separate list of Boolean values. This matters when your script validates thousands of rows from a large CSV file.
If you often process CSV data, this guide on reading large CSV files in Python is a useful next step.
Check Whether Every Item Is a String
You may need to confirm that every customer name in a contact-export script is a string.
customer_names = ["Emily Carter", "Michael Davis", "Sophia Wilson"]
all_names_are_strings = all(
isinstance(name, str) for name in customer_names
)
print(all_names_are_strings)
Output:
True
The isinstance() function checks an object’s data type. In this case, Python checks whether each value is a string.
Now include a number by mistake:
customer_names = ["Emily Carter", "Michael Davis", 1042]
all_names_are_strings = all(
isinstance(name, str) for name in customer_names
)
print(all_names_are_strings)
Output:
False
This type of validation helps before you format names, save data to a file, or send a customer email.
For more examples of checking values and types, read how to check if a Python variable is an integer.
Check Whether Every List Item Has Content
A small automation script may collect report columns such as employee name, department, and email address. Before writing a row to a CSV file, confirm that none of the required values are blank.
employee_record = ["Daniel Brown", "Finance", "daniel.brown@example.com"]
record_is_complete = all(employee_record)
print(record_is_complete)
Output:
True
Every string contains text, so all() returns True.
Now test an incomplete record:
employee_record = ["Daniel Brown", "", "daniel.brown@example.com"]
record_is_complete = all(employee_record)
print(record_is_complete)
Output:
False
The empty department value fails the test.
This is a good quick validation pattern when non-empty values are enough. If you need to validate the format of an email address, use a separate rule as well. You can learn more in this guide on validating email addresses in Python.
Python all() Function With Dictionaries
When you pass a dictionary directly to all(), Python checks the dictionary’s keys, not its values.
Here is an example:
employee_status = {
"name": "Megan Taylor",
"department": "Operations",
"email": "megan.taylor@example.com"
}
print(all(employee_status))Output:
True
The keys "name", "department", and "email" are all non-empty strings.
However, this does not confirm that the values contain data:
employee_status = {
"name": "Megan Taylor",
"department": "",
"email": "megan.taylor@example.com"
}
print(all(employee_status))Output:
True
The result remains True because Python checks only the keys.
To check values, use the dictionary’s .values() method:
employee_status = {
"name": "Megan Taylor",
"department": "",
"email": "megan.taylor@example.com"
}
all_values_present = all(employee_status.values())
print(all_values_present)Output:
False
The empty department value makes the result False.
Validate Required Dictionary Keys
In real API and JSON processing, I usually validate required keys before accessing them. This prevents a KeyError, which happens when code tries to read a dictionary key that does not exist.
employee_data = {
"name": "Robert Miller",
"email": "robert.miller@example.com",
"department": "Sales"
}
required_fields = ["name", "email", "department"]
has_required_fields = all(
field in employee_data for field in required_fields
)
print(has_required_fields)Output:
True
The code checks whether every required field exists in the dictionary.
Now remove the department key:
employee_data = {
"name": "Robert Miller",
"email": "robert.miller@example.com"
}
required_fields = ["name", "email", "department"]
has_required_fields = all(
field in employee_data for field in required_fields
)
print(has_required_fields)Output:
False
This technique is especially useful after loading JSON data. You can also review how to work with JSON data in Python when building API or file-processing scripts.
Pro Tip: In my automation projects, I validate required keys first and values second. A key may exist with an empty value, so
all(field in data for field in required_fields)andall(data[field] for field in required_fields)solve different problems.
Python all() Function for Data Validation
Let’s build a complete but small example. Imagine a local Python script that checks employee rows before it writes a monthly bonus report.
Each employee needs:
- A non-empty name
- A company email address
- A positive sales amount
- An active status
employees = [
{
"name": "Olivia Johnson",
"email": "olivia.johnson@example.com",
"sales": 18500,
"active": True
},
{
"name": "Ethan Anderson",
"email": "ethan.anderson@example.com",
"sales": 14200,
"active": True
},
{
"name": "Grace Martinez",
"email": "grace.martinez@example.com",
"sales": 9100,
"active": True
}
]
def is_valid_employee(employee):
required_keys = ["name", "email", "sales", "active"]
has_required_keys = all(
key in employee for key in required_keys
)
has_valid_values = all([
employee.get("name"),
employee.get("email"),
"@" in employee.get("email", ""),
isinstance(employee.get("sales"), (int, float)),
employee.get("sales", 0) > 0,
employee.get("active") is True
])
return has_required_keys and has_valid_values
all_employees_valid = all(
is_valid_employee(employee) for employee in employees
)
print(all_employees_valid)
Output:
True
This code defines a function, which is a reusable block of code. The is_valid_employee() function validates one employee dictionary.
Then the final all() call checks whether every employee passes the validation function.
Now add an invalid employee record:
employees = [
{
"name": "Olivia Johnson",
"email": "olivia.johnson@example.com",
"sales": 18500,
"active": True
},
{
"name": "Ethan Anderson",
"email": "ethan.anderson@example.com",
"sales": 14200,
"active": True
},
{
"name": "Grace Martinez",
"email": "",
"sales": 0,
"active": True
}
]
def is_valid_employee(employee):
required_keys = ["name", "email", "sales", "active"]
has_required_keys = all(
key in employee for key in required_keys
)
has_valid_values = all([
employee.get("name"),
employee.get("email"),
"@" in employee.get("email", ""),
isinstance(employee.get("sales"), (int, float)),
employee.get("sales", 0) > 0,
employee.get("active") is True
])
return has_required_keys and has_valid_values
all_employees_valid = all(
is_valid_employee(employee) for employee in employees
)
print(all_employees_valid)
Output:
False
This approach works well for small scripts, command-line tools, scheduled jobs, and data-cleaning tasks.
If you want to save validated information for another script, see how to write JSON data to a file in Python.
Python all() Function vs any()
Python provides both all() and any(), but they answer different questions.
- Use all() when every condition must pass.
- Use any() when at least one condition must pass.
Here is a practical comparison:
report_statuses = ["completed", "completed", "completed"]
all_completed = all(
status == "completed" for status in report_statuses
)
any_completed = any(
status == "completed" for status in report_statuses
)
print("All reports completed:", all_completed)
print("At least one report completed:", any_completed)
Output:
All reports completed: True
At least one report completed: True
Now use mixed statuses:
report_statuses = ["completed", "pending", "failed"]
all_completed = all(
status == "completed" for status in report_statuses
)
any_completed = any(
status == "completed" for status in report_statuses
)
print("All reports completed:", all_completed)
print("At least one report completed:", any_completed)
Output:
All reports completed: False
At least one report completed: True
Use all() when your script must stop on any invalid record. Use any() when a single matching record is enough to continue.
For example, a file-cleanup script may use any() to determine whether any files match a condition, while a report validation script may use all() to ensure every row is correct.
How all() Stops Early
The Python all() function uses short-circuit evaluation. This means Python stops checking as soon as it finds a falsy result.
Here is a demonstration:
def check_report_section(section_name, is_ready):
print(f"Checking {section_name}")
return is_ready
report_is_ready = all([
check_report_section("Sales", True),
check_report_section("Inventory", False),
check_report_section("Payroll", True)
])
print("Report ready:", report_is_ready)
Output:
Checking Sales
Checking Inventory
Checking Payroll
Report ready: False
This example evaluates every function before all() receives the list. That means it does not benefit from early stopping.
To allow short-circuit evaluation, use a generator expression:
def check_report_section(section_name, is_ready):
print(f"Checking {section_name}")
return is_ready
sections = [
("Sales", True),
("Inventory", False),
("Payroll", True)
]
report_is_ready = all(
check_report_section(section_name, is_ready)
for section_name, is_ready in sections
)
print("Report ready:", report_is_ready)
Output:
Checking Sales
Checking Inventory
Report ready: False
Python stops after Inventory returns False. It never checks Payroll.
This difference matters when each check reads a file, runs a database query, calls an API, or processes a large dataset. Prefer a generator expression when you only need the final answer.
Pro Tip: I’ve found generator expressions especially useful in file-processing jobs. They keep memory usage lower and stop work early as soon as one invalid record appears.
Things to Keep in Mind
- Empty iterables return True:
all([])returnsTrue, so explicitly check for records when an empty input should fail. - Dictionaries check keys by default: Use
all(data.values())when you need to validate dictionary values rather than keys. - Use comparisons for real rules:
all(sales)only checks whether sales values are non-zero. Useall(amount > 0 for amount in sales)for a clear business rule. - Prefer generators for large data: Use
all(condition for item in items)instead of building a full Boolean list when processing large files or data streams. - Handle missing dictionary values safely: Use
.get()with a default value when input data may not contain every key. - Do not hide complex validation: A long
all([...])statement can become difficult to debug. Move complex checks into a well-named function.
Frequently Asked Questions
What does all() do in Python?
The Python all() function returns True when every item in an iterable is truthy. It returns False when it finds at least one falsy item, such as False, 0, an empty string, or None.
Why does all([]) return True in Python?
all([]) returns True because the empty list contains no falsy values. Python treats the statement “every item passes” as true when no item exists that fails.
Can I use all() with a list of numbers?
Yes. Python treats non-zero numbers as truthy and zero as falsy. Use a comparison expression such as all(number > 0 for number in numbers) when you need a specific numeric rule.
Does all() work with dictionaries in Python?
Yes, but all(dictionary) checks dictionary keys. Use all(dictionary.values()) to check values, or use all(key in dictionary for key in required_keys) to check required keys.
What is the difference between all() and any() in Python?
Use all() when every condition must be true. Use any() when one or more conditions may be true. For example, all(score >= 60 for score in scores) checks whether every score passes.
Is all() faster than a for loop in Python?
all() is usually concise and efficient because it stops after the first falsy result when you pass a generator expression. A manual loop can do the same job, but all() often makes the intent clearer.
The Python all() function gives you a clean way to confirm that every value or condition passes before your script continues. Start with simple lists, then use generator expressions and reusable validation functions as your automation scripts grow.
I hope you found this practical guide helpful and feel ready to use all() in your next Python project.
You May Also Like
- Python map() function explained with examples
- Python zip() function for combining iterables
- How to filter lists in Python
- How to define a function in Python
- How to catch multiple exceptions in Python

Bijay Kumar is an experienced Python and AI professional who enjoys helping developers learn modern technologies through practical tutorials and examples. His expertise includes Python development, Machine Learning, Artificial Intelligence, automation, and data analysis using libraries like Pandas, NumPy, TensorFlow, Matplotlib, SciPy, and Scikit-Learn. At PythonGuides.com, he shares in-depth guides designed for both beginners and experienced developers. More about us.