How to Check if a Variable is Not Empty in Python?

Recently, while working with variables in Python, I was required to check if a variable is not empty before performing any operations. I tried different methods to do so. In this tutorial, I will show you various methods to check if a variable is not empty in Python with real examples.

To check if a variable is not empty in Python using the simplest method, you can use an if statement. In Python, empty values such as None0'' (empty string), [] (empty list), and {} (empty dictionary) are considered False. Therefore, you can simply write if variable: to check if the variable is not empty. For example, name = "John Doe"; if name: print("The name is not empty.") will print “The name is not empty” because the string name is not empty.

Now, let me show you each method with examples.

1. Using the if Statement

The best way to check if a variable is not empty is by using an if statement in Python. In Python, empty values such as None, 0, '' (empty string), [] (empty list), and {} (empty dictionary) are considered False in a boolean context. Here is an example of how you can use an if statement to check for non-empty variables in Python:

# Example with a string
name = "John Doe"

if name:
    print(f"The name {name} is not empty.")
else:
    print("The name is empty.")

In this example, the variable name contains the string “John Doe”. Since the string is not empty, the if statement evaluates to True, and the message “The name John Doe is not empty.” is printed.

You can see the output in the screenshot below after I executed the above Python code using a Python editor.

Python check if variable is not empty

Check out Check if a Variable is an Empty String in Python

2. Using the len() Function

Let me show you another useful method in Python to check if a variable is not empty, and that is the len() function.

To check if a variable is not empty, you can use the len() function, which returns the length of an object. If the length is greater than zero, the variable is not empty.

Here is the complete Python code.

# Example with a list
cities = ["New York", "Los Angeles", "Chicago"]

if len(cities) > 0:
    print("The list of cities is not empty.")
else:
    print("The list of cities is empty.")

Here, the variable cities is a list containing three city names. The len() function returns 3, which is greater than zero, so the message “The list of cities is not empty.” is printed.

After I executed the above Python code, it showed me the exact output, as you can see in the screenshot below:

Check if a Variable is Not Empty in Python

Read Check if a Variable is Not Null in Python

3. Using the bool() Function

The bool() function converts a value to a boolean in Python. This can be useful for checking if a variable is not empty. Here is an example to understand it better way.

# Example with a dictionary
states = {"CA": "California", "NY": "New York"}

if bool(states):
    print("The dictionary of states is not empty.")
else:
    print("The dictionary of states is empty.")

In this example, the variable states is a dictionary with two key-value pairs. The bool() function returns True because the dictionary is not empty, so the message “The dictionary of states is not empty.” is printed.

You can see the output in the screenshot below:

Check if a Variable is Not Empty in Python using bool method

4. Using the not Operator

You can also use the not operator to check if a variable is not empty in Python. The not operator inverts the boolean value of a variable. Let me show you an example to understand it better way.

# Example with a set
zip_codes = {90210, 10001, 60601}

if not not zip_codes:
    print("The set of zip codes is not empty.")
else:
    print("The set of zip codes is empty.")

Here, the variable zip_codes is a set containing three zip codes. The not not expression evaluates to True because the set is not empty, so the message “The set of zip codes is not empty.” is printed.

You will also get the exact same output when you run the above Python code like in the screenshot below:

How to Check if a Variable is Not Empty in Python

Check out Check if a Variable is NaN in Python

5. Using List Comprehensions and Generators

For more complex data structures, such as lists of dictionaries, you can use list comprehensions or generators to check if they are not empty:

Here is an example.

# Example with a list of dictionaries
people = [{"name": "Alice", "city": "San Francisco"}, {"name": "Bob", "city": "Seattle"}]

if any(person for person in people):
    print("The list of people is not empty.")
else:
    print("The list of people is empty.")

In this example, the variable people is a list of dictionaries. The any() function checks if there is at least one non-empty dictionary in the list, which there is, so the message “The list of people is not empty.” is printed.

Conclusion

In this tutorial, I have explained how to check if a variable is not empty in Python using various methods with examples. I hope all these examples will help you to understand it better. Various methods we tried are using if statements, the len() function, the bool() function, the not operator, and list comprehensions or generators, etc, to know if a variable contains data.

Do let me know if you have any questions in the comments below.

You may like the following tutorials:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.