Quantcast
Channel: Planet Python
Viewing all articles
Browse latest Browse all 23741

Python Morsels: Checking for an empty list in Python

$
0
0

Python programmers typically check for empty lists by relying on truthiness.

Checking the length of a list

One way to check whether a list is empty is to check the length of that list. If the length is 0, the list must be empty:

>>> numbers=[]>>> iflen(numbers)==0:... print("The list is empty.")...The list is empty.

Or if we wanted to check for non-empty lists, we could make sure that the length is greater than 0:

>>> iflen(numbers)>0:... print("The list is NOT empty.")...

But this is actually not the most typical way to check for an empty list in Python.

Evaluating the truthiness of a list

Many Python users prefer to …

Read the full article: https://www.pythonmorsels.com/checking-for-an-empty-list-in-python/


Viewing all articles
Browse latest Browse all 23741

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>