Python

How to check if a list is empty in Python

January 23, 2022

How to check if a list is empty in Python

In this article you will learn how to check if a list is empty in Python language, in a very simple way using native resources.

check if a list is empty in Python cover

What’s up  programmer, Are you okay? Let’s learn more about Python!

In Python we have a very simple way to check if a list is empty, and we don’t even need to use any method or library for it.

Check out  this example:

list = []

if not list:
 print("The list is empty.")

Simple isn’t it? 😀

We just need to apply the not operator, which actually checks for a value in the list.

Since False is returned, we will receive the response we want.

If there was something, it would return True and then we could do another logic for the list to have any elements

Pythonic code

Also note that even though this code works, it is not as explicit as the language preaches in its documentation, and that’s how the codes should be

If you want to understand more about pythonic code, read this article

In the article I comment on all the advantages for  following these pythonic standards  on your code

Conclusion

In this article we learned  how to check if a list is empty in the Python language

We use the NOT operator in an IF, this way the empty list returns a False and we can follow the logic of the empty list

Want to learn more about Python? Click here!

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x