How do you check if an object is empty in python?

In this short tutorial, find how to check if a list is empty in Python. We also look at why you need to do this so that you understand the purpose better.

How to check if a list is empty in Python?

Empty lists are considered False in Python, hence the bool[] function would return False if the list was passed as an argument. Other methods you can use to check if a list is empty are placing it inside an if statement, using the len[] methods, or comparing it with an empty list.

Table of Contents: Check if a list is empty in Python

  • Why do you check if a list is empty in Python?
  • Solution 1: Using PEP 8 recommended method
  • Solution 2: Using the bool[] function
  • Solution 3: Using len[]
  • Closing thoughts

Why do you check if a list is empty in Python?

While dealing with lists, a major characteristic most developers make use of is its iterability. This means that you can iterate through the values in the list making it suitable for loops especially for. This also comes in handy while working with strings and numerical operations. And hence it is a good practice to check if a list is empty before proceeding.

This remains true for all iterables i.e dictionaries, tuples, etc.

With that out of the way, let us look at the various methods that can be used to check if a list is empty in Python.

Solution 1: Using PEP 8 recommended method:

Solution 1 & 2 make use of a method called **Truth Value Testing**. What this essentially means is that we check if the list is empty using its boolean value. This is possible because, in Python empty sequences, positional arguments containing 0, 0.0 or with length 0, are all considered to be false. You can read more about this here.

Because of this method, we can check if a list is empty in Python. And below is the most Pythonic way of checking the same.

l1 = ["Hire", "the", "top", "1%", "freelancers"]

l2 = []

if l2:
    print["list is not empty"]
else:
    print["list is empty"]

#Output: "list is empty"

Since an empty list is False, the condition is false and hence we are able to identify an empty list. Feel free to change the condition with l1.

Another common method is with the Implication of a not.

sl1 = ["Hire", "the", "top", "1%", "freelancers"]

l2 = []

if not l2:
    print["list is  empty"]
else:
    print["list is not empty"]

# Output: "list is empty"

This is a similar approach however we use a not in the loop. It inverses the value and hence the condition becomes true. This method is used to increase readability as a developer could type the desired code under the else.

Solution 2: Using the bool[] function

Similar to the first method, we check if a list is empty using the bool[] function. The bool[] function returns the boolean value of an object i.e true or false. The code is also very similar to the first method. Choosing between the two methods would boil down to a personal choice.

l1 = ["Hire", "the", "top", "1%", "freelancers"]

l2 = []

if bool[l2]:
    print["list is empty"]
else:
    print["list is not empty"]

# Output: "list is empty"

And since the value is false the print under the else is returned.

Solution 3: Using len[]

In this solution, we use the len[] to check if a list is empty, this function returns the length of the argument passed. And given the length of an empty list is 0 it can be used to check if a list is empty in Python.

Here again, there are two techniques that can be used. The first method is based on the Truth Value Testing, 0 is considered false.

l1 = ["Hire", "the", "top", "1%", "freelancers"]

l2 = []

if len[l2]:
    print["list is not empty"]
else:
    print["list is empty"]

# Output: "list is empty"

Here since the len[] of l2 is 0 it is considered false and hence the condition returns the output under the else.

In the other methods, we use a condition to compare the length of the list with 0. Although this method is very similar to the first method this is mainly used to help improve readability.

l1 = ["Hire", "the", "top", "1%", "freelancers"]

l2 = []

if len[l2] == 0:
    print["list is empty"]
else:
    print["list is not empty"]

# Output: "list is empty"

And since the condition is true, it returns the first value.

Closing thoughts

As you have seen there are multiple ways through which you can check if a list is empty in Python. And it is a good practice to use this condition before and then nest your if or for loops, this would help reduce unwanted errors.

And as to which solution would be the best choice, it again boils down to your knowledge of the language. If you are a beginner I would suggest you use the methods with the len[ ]== 0 as it is straightforward and readable. If you are proficient you can use solutions 1 or 2 but again I would recommend using the not l2 solution as it is more readable.

How do you check if all values in a list are empty Python?

Use the all[] function to check if all items in a list are None in Python, e.g. if all[i is None for i in my_list]: . The all[] function takes an iterable as an argument and returns True if all of the elements in the iterable are truthy [or the iterable is empty].

How do you check if a set is empty?

Set. isEmpty[] method is used to check if a Set is empty or not. It returns True if the Set is empty otherwise it returns False.

How do you check if a stack is empty in Python?

isEmpty[] – Return True if the stack is empty, False otherwise. peek[] – Return the top item in the stack. If the stack is empty, raise an exception.

How do you check for an empty array in Python?

How to check if a NumPy array is empty in Python.
empty_array = np. array[[]].
is_empty = empty_array. size == 0..
print[is_empty].
nonempty_array = np. array[[1, 2, 3]].
is_empty = nonempty_array. size == 0..
print[is_empty].

Chủ Đề