How to check if list is empty Python?

In this article, we will learn how to check if the list is empty in python?

Contents

3 Ways to the checklist

Length

We can check if a list is empty utilizing the length of the list.

It’s an easy solution and the vast majority get it as a first methodology.

How about we see the means to check the run the list empties.

Compose a function called is_list_empty that accepts a rundown as an argument.

Check the length of the list.

On the off chance that the length is 0, return True else return False.

That is it We are finished with the means associated with the program.

Code:

Function to check whether the list is empty or not

```
Def is_list_empty(list):

# checking the length

If len(list) == 0:

# returning true as length is 0

Return True

# returning false as length is greater than 0

Return False

List_one = [1, 2, 3]

List_two = []

Print(is_list_empty(list_one))

Print(is_list_empty(list_two))

```

Output:

False

True

Bool:

The boolean value of an empty list is in every case False.

Here, we will exploit the bool technique.

We will utilize the bool change technique to check if the rundown is vacant.

How about we see the means associated with it.

Compose a function called is_list_empty that accepts a list as an argument.

Convert the list to boolean utilizing the bool technique.

Inver the outcome and return it

Code:

Function to check whether the list is empty or not

```
Def is_list_empty(list):

# returning boolean value of current list

# empty list bool value is False

# non-empty list boolea value is True

Return not bool(list)
<h3>Function Test:</h3>
List_one = [1, 2, 3]

List_two = []

Print(is_list_empty(list_one))

Print(is_list_empty(list_two))

```

You will get the same output as we have seen in the previous example.

Execute and test it.

Equality Operator:

There is another basic method to check the list is vacant or not.

We can straightforwardly contrast the list and empty list([]).

Python returns True if the given list matches with the vacant list.

How about we see the means to check if the list is unfilled with the equality Operator.

Compose a function called is_list_empty that accepts a rundown as an argument.

Contrast the given list and [] and return the list

One straightforward step gives you a great deal in Python.

We should see the code.

Code:

Function to check whether the list is empty or not

```
Def is_list_empty(list):

# comparing the list with []

# and returning the result

Return list == []

```

You can check the function with the code snipped that we have used in this tutorial.

You will get the same output as before.

Tags: