Tutorials  Articles  Notifications  Login  Signup


VS

Vishal Saxena

SDE 1 at Amazon April 11, 2020, 3:16 p.m. ⋅ 1333 views

Python - check if a list is empty ?


To check if a list is empty or not we simple put the list variable inside if condition. If the list is not empty it will be treated as true and code inside if block will be executed, otherwise code inside if block will not be executed.

 

myList = []  # This is a empty list

if myList:
	# since myList is empty this not be executed.
	print("List is not empty")
else:
	print("List is empty")

 

Executing if block only if list is empty can be done using not operator before list.

newList = []

if not newList:
	print("list is not empty")

 



HackerFriend Logo

Join the community of 1 Lakh+ Developers

Create a free account and get access to tutorials, jobs, hackathons, developer events and neatly written articles.


Create a free account