How to check if the given list is empty in Python ?
This tutorial guides you on how to check if the given list is empty in Python programming language using if condition and Boolean operation not.
Check if the given list is empty in Python
For example, the following is the given input list.
>>> l = [[1, 2, 3], [4, 5, 6], , [8, 9]]
To check if the given list is empty or not using Python, try running the following logic which uses Boolean operation (not l) as operand in if condition.
if not l: print('list is empty') else: print('list is not empty')
list is not empty
Boolean Operations – and, or, not
The following are the Boolean operations, ordered by ascending priority. Any object can be tested for truth value (true/ false) using if or while condition with any Boolean operations as operand as show in the example above.
Operation Result x or y if x is false, then y, else x x and y if x is false, then x, else y not x if x is false, then True, else False
using len() in if condition
You may think to check length of the given list explicitly and check whether it contains elements or it is empty as shown below.
Don’t Do : For example,
>>> l =  if len(l)==0: print('list is empty') else: print('list is not empty')
list is empty
But that’s not the best practice.
Do this : Instead check if the given list is empty or not in the following ways: using len() function and Boolean operations as shown below.
#given input list -empty list >>> l =  #using len() function alone >>> if len(l): ... print('list is not empty') ... else: ... print('list is empty') ... list is empty #using len() and Boolean operations >>> if not len(l): ... print ('list is empty') ... else: ... print('list is not empty') ... list is empty
That’s it. You had learnt the best practice to check if the list is empty or not. Also, learnt how to use Boolean operations in if condition.
Hope it is helpful 🙂
You’ll also like:
- Convert list of lists to flat list – flatten list in Python ?
- String Formatting in Python – Examples
- Single digit number to double digits string conversion in Python
- String Immutability with Examples in Python
- Python Program to Check Given Number is Odd or Even
- Increase the cell width of the Jupyter Notebook in browser
- Add python3 kernel to jupyter IPython notebook ?
- Programs to Print Patterns – Pyramid, Triangle using Star
- How to change the default theme in Jupyter Notebook ?
- Change the Jupyter Notebook startup folder in Windows & Mac
- To run Jupyter Notebook on Windows from command line
- String Slicing in Python with Examples
- Python program to find the greatest of three numbers
- Find difference between two given numbers in Python
- String indexing in Python with Examples
- Embed HTML within IPython Notebook
- Difference between Python’s list methods append and extend ?
- Install Python 3 on Windows 10 machine
- Convert negative to positive number in Python
- Create list of lists in Python
- Extract numbers from a string in python
- How to access index and value in for loop – Python?
- Find index of an element in a list – Python