How do I check if a list is empty in Python?
How do I check if a list is empty in Python?
631
27-Apr-2023
Aryan Kumar
01-May-2023In Python, you can check if a list is empty using the built-in len() function, or by checking the boolean value of the list itself. Here are two examples:
In the first example, the len() function is used to check the length of the list. If the length is equal to 0, then the list is empty.
In the second example, the boolean value of the list is checked. An empty list has a boolean value of False, so the not operator is used to check if the list is empty.
Both methods are equivalent, and you can use whichever method you prefer.
Gulshan Negi
29-Apr-2023Hello this is Gulshan Negi
Well, using Python's built-in len() function or the list's boolean value, you can determine whether a list is empty.
Here's an example using the len() function:
Hope it will work for you
Thanks
Krishnapriya Rajeev
28-Apr-2023We can use the Pythonic method of checking if a list is empty using its implicit booleanness. This is shown below in the case of an empty list:
If the list is not empty: