# Using the "all" function in Python

Are you tired of writing a long conditional statement with repetitive and statements to confirm if certain values are not None, empty, 0 or even False? if yes, this article is for you.

Discovering any() and all() for me was time-saving, concise and neat for me. all() checks if all elements of an iterable has no falsy value while any() checks if any element of an iterable is falsy, both return a boolean after operation. However, this article covers only the all() function.

# all()

For example, as a Django web developer, while building an API endpoint for registration, certain request parameters that is required

user_email = request.data.get("email", None)
user_tel = request.data.get("tel_number", None)
user_password = request.data.get("password", None)
user_fullname = request.data.get("full_name", None)
user_address = request.data.get("house_address", None)
user_state = request.data.get("state", None)
user_country = request.data.get("country", None)

All of these requests variables are required, and to validate if the variables were not None, empty or false, I would traditionally find myself writing something like this

if (user_email is not None and user_tel is not None and user_password is not None and user_fullname is not None and user_address is not None and user_state is not None and is not None and user_country is not None):

This looks lengthy, i haven't even checked if each of the variables is empty or an empty string "", meaning it could be longer. Until i discovered the all() function.

Using all()

#list all required varaibles in an iterable data structure like list or tuple
required_requests = (user_email, user_tel, user_password, user_fullname, user_address, user_state, user_country)

if all(required_requests):

The code block above is shorter, readable and does a better job than the previous block because it checks for falsy values like None, empty string, 0 and False.

Thanks for reading

More from SoftNexus