While list is not empty python

Example 1: Using Boolean operation

my_list = []
if not my_list:
    print("the list is empty")

Output

the list is empty

If my_list is empty then not returns True.

It is the most pythonic way of testing emptiness. If you want to learn more about boolean truth value, you can refer to Truth Value Testing.


Example 2: Using len()

my_list = []
if not len(my_list):
    print("the list is empty")

Output

the list is empty

In this example, length of list is used to check if there is any element in the list. If the length of a list is 0, then the list is empty.


Example 3: Comparing with []

my_list = []
if my_list == []:
    print("The list is empty")

Output

The list is empty

[] is an empty list, therefore if my_list has no elements, then it should be equal to [].


In Python, a list is an ordered sequence that can hold several object types such as integer, character, or float.

In this article, we will show you how to check if the given input list is an empty list or NOT using python. Below are the 5 methods to accomplish this task −

  • Using not operator

  • Using len() function

  • By Comparing with an Empty List

  • Using __len__()

  • Using NumPy Module

Assume we have taken an empty list. We will check if the input list is empty or not and returns some random message for acknowledgment using different methods as specified above.

Method 1: Using not operator

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a variable to store the input empty list.

  • Check if the list is empty or not by using if condition and not operator

  • If the list is empty then (not list) returns True

  • If the (not list) returns False then the list is not empty.

Example

The following program checks whether the input list is empty or not using the not operator −

lst = [1,2,3,4,5] if not lst: print('Empty list') else: print('List is not Empty \n',lst)

Output

On executing, the above program will generate the following output −

('List is not Empty \n', [1, 2, 3, 4, 5])

Method 2: Using len() function

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a variable to store the input empty list.

  • Check whether the length of the list is equal to 0 using the len() function(The number of items in an object is returned by the len() method. The len() function returns the number of characters in a string when the object is a string) in an if conditional statement.

  • Print Empty list, If the length of the list is equal to 0.

  • Else print List is not empty.

Example

The following program checks whether the input list is empty or not using the len() function −

lst = [] if len(lst) == 0: print('Empty list') else: print('Not Empty list')

Output

On executing, the above program will generate the following output −

Empty list

Method 3: By Comparing with an Empty List

[] This denotes an empty list. So, by comparing our list object to [], we can determine whether the list is empty or not.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a variable to store the input empty list.

  • Use the if conditional statement to check whether the list object is pointing to the literal [] i.e checking whether the list is equal to [] or not.

  • Print “Empty list”, if the condition is true.

  • Else print “List is not empty”, if the condition is false.

Example

The following program checks whether the input list is empty or not using the empty list[] literal −

lst = [] if lst == []: print('Empty list') else: print('List is not empty\n',lst)

Output

On executing, the above program will generate the following output −

Empty list

Method 4: Using __len__()

The size of a list can be obtained by invoking the __len__() function on the list object. The list is empty if the list size equals zero.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a variable to store the input empty list.

  • Use the if conditional statement to check whether the length of the list is equal to 0 using the __len__() function(The __len__ function in Python returns a positive integer representing the length of the object on which it is called. It implements the inbuilt len() function)

  • Print “Empty list”, If the list length is equal to 0.

  • Else print “List is not Empty”.

Example

The following program checks whether the input list is empty or not using the __len__() function −

lst = [] if lst.__len__() == 0: print('Empty list') else: print('Not Empty list')

Output

On executing, the above program will generate the following output −

Empty list

Method 5: Using NumPy Module

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Use the import keyword to import the numpy module

  • Create a variable to store the input empty list.

  • Use the numpy.array() function to convert a list to NumPy array and create a variable to store it.

  • Use the if conditional statement to check whether the numpy array size is equal to 0 with the size attribute.

  • Print “Empty list”, if the condition is true.

  • Else print “List is not Empty”, if the condition is false.

Example

The following program checks whether the input list is empty or not using the NumPy module and size attribute −

import numpy as np lst = [] resultarray = np.array(lst) if resultarray.size == 0: print('Empty list') else: print('List is not Empty')

Output

On executing, the above program will generate the following output −

Empty list

Conclusion

This article taught us how to use the not operator to determine whether a statement is true or false. To determine the length of a list, we learned how to use the len() function. The length of a tuple, dictionary, string, etc. can be determined using this. Additionally, we learnt how to create a NumPy array from a list and how to determine the size and length of a NumPy array.

While list is not empty python

Updated on 19-Sep-2022 09:35:54

  • Related Questions & Answers
  • How to check if a C# list is empty?
  • What is best way to check if a list is empty in Python?
  • Check if a list is not empty in MongoDB?
  • Python - Check if dictionary is empty
  • How to check if a Laravel collection is empty?
  • How to check if a string is empty in Kotlin?
  • How to check if android editText is empty?
  • Python Pandas - Check if an interval is empty
  • How to check if String is empty in Java?
  • How to check if PSCustomObject is empty in PowerShell?
  • Python program to check whether a list is empty or not?
  • Check if a HashMap is empty in Java
  • Python - Check if a list is contained in another list
  • Check if value is empty in JavaScript
  • How to Check if Android EditText is empty in Kotlin?

How do you check if a list is not empty in Python?

In this solution, we use the len() to check if a list is empty, this function returns the length of the argument passed. And given the length of an empty list is 0 it can be used to check if a list is empty in Python.

What is a non empty list in Python?

In Python, empty lists evaluate False , and non-empty lists evaluate True in boolean contexts. Therefore, you can simply treat the list as a predicate returning a Boolean value. This solution is highly Pythonic and recommended by the PEP8 style guide.

How do you check if a list is empty?

You can also compare the list object with the empty list to check if the list is empty. An empty list is denoted using the [] . When a list object is compared with [] using == operator, then it returns True if the list object is empty. Else it returns False .

How check array is empty or not in Python?

Use the if conditional statement to check whether the numpy array size is equal to 0 with the size attribute. Print “Empty list”, if the condition is true. Else print “List is not Empty”, if the condition is false.