Post

PythonPython

A
Aryan Sharma

Posted on 8th May 2024|50 views

0
votes

Python Is Empty

How to check whether a list is empty or not in python?

Answers
P
victoria mamidi

Posted on 8th May 2024

I will explain it in two ways.

1. This is an example of checking a list is empty or not in a less pythonic way.

def Enquiry(li1):

    if len(li1) == 0:

        return 0

    else:

        return 1

         li1 = []

if Enquiry(li1):

    print ("not an empty list")

else:

    print("List is empty")

2. Now let us view a more pythonic method to check for an empty list. That method of a check means an implicit method of checking also more excellent than the earlier one.

def Enquiry(li1):

    if not li1:

        return 1

    else:

        return 0

      li1 = []

if Enquiry(li1):

    print ("list is Empty")

else:

    print ("list is not empty")

 

Write your answer

STILL GOT QUERIES?

Get a Live FREE Demo
  • Explore the trending and niche courses and learning maps
  • Learn about tuition fee, payment plans, and scholarships
  • Get access to webinars and self-paced learning videos