Python tuple, list and dict are widely used to save data in python application. How to check they are empty? To answer this question. We will write some examples to explain it for you in this tutorial.
An empty tuple, list and dict
In python script, we can create an empty tuple, list and dict like this:
tuple = () list = [] dict = {}
Python tuple, list and dict are empty, which means there is not any data in them.
How to check tuple, list and dict is empty or not
The most simplest way is:
if tuple: print("tuple is not empty") if list: print("list is not empty") if dict: print("dict is not empty")
While, you also can check them by their length. Here is an example:
if len(tuple) > 0: print("tuple is not empty") if len(list) > 0: print("list is not empty") if len(dict) > 0: print("dict is not empty")