๐ Mastering any(), not any(), all(), and not all() in Python with Real-Life Examples
PYTHON
5/5/20253 min read


Python provides a set of built-in functions like any(), not any(), all(), and not all() that can simplify conditional checks, making your code more readable and efficient. In this post, we'll dive into these functions with practical, real-world examples to understand how to use them effectively.
๐น What is any() in Python?
The any() function returns True if at least one element in an iterable is True. If all elements are False, it returns False.
โ Real-Life Example: Checking If a Student Has Completed Any Assignments
Imagine you are managing a list of students and their assignment completion status:
students = [
{"name": "Alice", "assignments": [True, False, True]},
{"name": "Bob", "assignments": [False, False, False]},
{"name": "Charlie", "assignments": [True, True, True]}
]
for student in students:
if any(student["assignments"]): # Checks if any assignment is True (completed)
print(f"{student['name']} has completed at least one assignment.")
else:
print(f"{student['name']} has not completed any assignments.")
OUTPUT:
Alice has completed at least one assignment.
Bob has not completed any assignments.
Charlie has completed at least one assignment.
๐น What is not any()?
not any() returns True if none of the elements in the iterable are True (i.e., all are False).
โ Real-Life Example: Checking If Any Item in Stock is Available
Consider an online store with a list of products. You need to check if any product is available in stock.
products = [
{"name": "Laptop", "in_stock": False},
{"name": "Smartphone", "in_stock": False},
{"name": "Headphones", "in_stock": False}
]
if not any(product["in_stock"] for product in products):
print("All products are out of stock!")
else:
print("At least one product is available.")
OUTPUT:
All products are out of stock!
๐น What is all() in Python?
The all() function returns True only if every element in the iterable is True. If any element is False, it returns False.
โ Real-Life Example: Checking If All Tasks Are Completed
Imagine a project management tool where each task has a boolean value indicating whether itโs completed. You want to check if all tasks are completed before moving on to the next phase of the project.
tasks = [True, True, True] # True means completed, False means pending
if all(tasks):
print("All tasks are completed. Ready for the next phase!")
else:
print("Some tasks are still pending.")
OUTPUT:
All tasks are completed. Ready for the next phase!
๐น What is not all()?
not all() returns True if any element is False. If all elements are True, it returns False.
โ Real-Life Example: Checking If Any Employee is Missing from a Meeting
Imagine you have a list of employees scheduled to attend a meeting, and you want to check if anyone is missing. If even one person is absent, the meeting canโt proceed as planned.
employees = [
{"name": "Alice", "attending": True},
{"name": "Bob", "attending": True},
{"name": "Charlie", "attending": False}
]
if not all(employee["attending"] for employee in employees):
print("Not all employees are attending the meeting. Reschedule it!")
else:
print("All employees are attending the meeting.")
OUTPUT:
Not all employees are attending the meeting. Reschedule it!
๐ Summary: When to Use Each Function?


๐งฉ Additional Real-World Examples:
Example 1: Checking for Missing Data in a List of User Profiles
Letโs say you are validating user profiles, and you need to check if any user is missing key information (like their email address or phone number).
user_profiles = [
{"name": "Alice", "email": "alice@example.com", "phone": "123-456-7890"},
{"name": "Bob", "email": "", "phone": "098-765-4321"},
{"name": "Charlie", "email": "charlie@example.com", "phone": ""}
]
missing_info_count = 0
for profile in user_profiles:
if not all(profile.values()): # Checks if any key is missing (empty string or None)
missing_info_count += 1
print(f"Number of profiles with missing information: {missing_info_count}")
OUTPUT:
Number of profiles with missing information: 2
Example 2: Ensuring All Items Are Below a Certain Price
Consider an e-commerce platform that needs to check if all items in a user's cart are below a specific price threshold before applying a discount.
cart_items = [
{"name": "Laptop", "price": 500},
{"name": "Smartphone", "price": 300},
{"name": "Headphones", "price": 50}
]
max_price = 400
if all(item["price"] <= max_price for item in cart_items):
print("All items are eligible for the discount!")
else:
print("Some items exceed the price threshold for a discount.")
OUTPUT:
Some items exceed the price threshold for a discount.
Conclusion
Using any(), not any(), all(), and not all() makes your Python code more concise, readable, and efficient. Whether you're handling data validation, checking conditions, or filtering lists, these functions allow you to solve problems in a clean and Pythonic way. By understanding when and how to use each of these functions, you can make your code both more efficient and easier to maintain.
Happy coding! ๐


Kishore Babu Valluri
Senior Data Scientist | Freelance Consultant | AI/ML & GenAI Expert
With deep expertise in machine learning, artificial intelligence, and Generative AI, I work as a Senior Data Scientist, freelance consultant, and AI agent developer. I help businesses unlock value through intelligent automation, predictive modeling, and cutting-edge AI solutions.