How to Check if a List is Empty in Python?

Spread the love

Problem –

You want to know if a list is empty or not.

Solution –

To check if a list is empty you can write

In [1]: x = []

In [2]: if not x:
   ...:     print('List is empty')
   ...:     
List is empty

or you can write

In [3]: x = []

In [4]: if len(x) == 0:
   ...:     print('List is empty')
   ...:     
List is empty

Rating: 1 out of 5.

Leave a Reply