• Home
  • Tutorials
  • Python Runner
  • Other
    • Become a Teacher
    • Profile
PythonBabuPythonBabu
  • Home
  • Tutorials
  • Python Runner
  • Other
    • Become a Teacher
    • Profile

Fundamental

  • Home
  • Fundamental
  • How to check list is empty or not?

How to check list is empty or not?

  • Posted by Python Babu
  • Date January 12, 2020
  • Comments 0 comment

Introduction

In this blog, you will know that, how can we check whether the list is empty or not in python.

Suppose you have a list like as follows

list = [];

As we know that this list is empty but it might be possible when you are getting the data from api or file or database etc. On that time list might be empty so while fetching data, we always check list is empty or not. So there are two ways to check which are as follows

1. Using the implicit booleanness

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

In above example, we are just check with the not operator to check that following example will clear you more about this

>>> list = []
>>> not list // This will print True
>>> list = [1, 2, 3, 4]
>>> not list // Now this will written False

2. Using len() function

We can also do the same using len() function, which checks the length of an list or string or tupple etc. example is as follows

if not len(list):
    print("List is empty")

In above example len() function will give the result in numbers. So if list will be empty the length of the list will be 0. After then we are using the not operator because 0 is equal to False in python but if condition works on True value.

Thank You 😊😊😊

Tag:empty, list, python

  • Share:
author avatar
Python Babu
Sourabh Somani is 4 times Microsoft MVP and 7-times C# Corner MVP, is a tech lead, contributor, International Speaker, Author, and the founder of www.pythonbabu.com. After possessing a B.Tech degree in Computer Science, he found his niche in core programming and database development. His core competencies include Mobile and web applications development using ASP.NET, Python, MVC, Node.js, C#, Java Script, jQuery, SQL Server, NoSQL, MongoDB, and Angular. With his extensive experience of several years, passion toward learning new technologies, and a zeal to give back to the developer community, Sourabh contributes his knowledge via very helpful articles and blogs on various community sites.

Previous post

Difference between remove, del, pop and clear in python list
January 12, 2020

You may also like

Difference between remove, del, pop and clear in python list
21 December, 2019

Introduction In this article, we will learn different methods to delete items from the python list. So there are 4 methods by which we can delete or remove elements from …

Conditional Expression in Python
15 December, 2019

Introduction In Python 2.5, Guido van Rossum added conditional expression. In Python there is no ternary conditional operator but by using Conditional Expression we can work like a Conditional Operator …

Leave A Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • How to check list is empty or not?
  • Difference between remove, del, pop and clear in python list
  • Conditional Expression in Python
  • Find all the Armstrong Numbers in a Range using python
  • Find Computer IP Address using Python

Recent Courses

Machine Learning with TensorFlow

Machine Learning with TensorFlow

Hello Friends, In this tutorial you will learn step by...
3 lessons
Python Tips & Tricks

Python Tips & Tricks

In this Course you will know different different Python Tips...
10 lessons
Python Tutorial for Beginners

Python Tutorial for Beginners

In this tutorial series you will get complete python beginner...
39 lessons
Go To All Recent Tutorials

Tags

armstrong number clear Conditional Expression del empty expression expressions ip ip address list pop python range

Copyright © Python Babu