| by Arround The Web | No comments

How to Check If a List Is Empty in Python

List” is a Python data structure that allows you to order items of any type. “Lists” are useful for storing and manipulating data, but sometimes you may need to check if a list is empty or not. It can be in a situation where there is a need to utilize the current list or append some entries to it based on a requirement.

How to Check/Determine if a List is Empty in Python?

To determine if a list is empty, the below-given methods are used in Python:

Method 1: Check if a List is Empty Utilizing the “if/else” Statement

The “if/else” statement is utilized to determine whether the specified list is empty or not.

Example

Here is an example code:

list1 = []
if list1:
    print("List is Not Empty")
else:
    print("List is Empty")

 

In the above code, the “if” statement is used to check if the initializer list does not contain any elements and the “else” block will only execute in the absence of all of the list items.

Output

Based on the above output, the input list is empty.

Method 2: Check if a List is Empty Utilizing the “bool()” Function

The “bool()” function is utilized to retrieve or transform a value to a Boolean value i.e., “True” or “False”. This function is utilized to verify/check whether the input list is empty or contains any value.

Syntax

bool([x])

 

In the above syntax, “x” is an optional parameter that can be any expression or value. If “x” is omitted, the function returns “False” by default.

Example

The below code is used to verify whether the list contains an element or not:

list1 = []
if bool(list1):
    print("List is Not Empty")
else:
    print("List is Empty")

 

According to the above code, the “bool()” function returns “True” and invokes the “if” statement if the initialized list contains some elements. In the other case, it returns “False” when there is an empty list, thereby executing the “else” statement.

Output

According to the above output, it can be implied that the input list is empty via the executed “else” statement.

Method 3: Check/Determine if a List is Empty Utilizing “len()” Function

The “len()” function is used to retrieve the total number of items included in the input iterable.

Syntax

len(obj)

 

Here, “obj” is any sequence such as list, str, dict, etc.

Example

The following code checks whether the initialized list is empty:

list1 = []
if len(list1):
    print("List is Not Empty")
else:
    print("List is Empty")

 

In the above code lines, the “len()” function is used to check the length of the input list. In the case of a longer length than “0”, it means that the list is not empty and the corresponding condition is executed based on that.

Output

Method 4: Check/Determine if a List is Empty Utilizing the “not” Operator

The “not” operator can also be used to check if a list contains the element or not based on a negation check.

Example

Here is an example code:

list1 = []
if not list1:
    print("List is Empty")
else:
    print("List is Not Empty")

 

According to the above code block, the “not” operator is utilized to return “True” when the empty list is present and “False” when the empty list does not exist instead. In this case, the “if” statement will come into effect since the list is empty and so the “not” operator returns “True”.

Output

Method 5: Check/Determine if a List is Empty by Comparing/Matching it With an Empty List

We can also compare the input list with the new initialized empty list “[ ]” to determine whether the input list is empty.

Example

The given example code is used to determine whether the input list contains elements or not:

list1 = []
if list1 == []:
    print("List is Empty")
else:
    print("List is Not Empty")

 

In the above code, the input list is compared with another empty list using the “==” operator and utilizing the “if” statement.

Output

Method 6: Check/Determine if a List is Empty Utilizing the “NumPy” Module

The “numpy” module function “numpy.array()” and the attribute “size” is used to check whether the input list is empty or not.

Example

The following example code is used to check for empty input lists:

import numpy
list1 = []
rslt_arr = numpy.array(list1)
if rslt_arr.size == 0:
   print('List is Empty')
else:
   print('List Contains Items')

 

According to this code block:

  • The “numpy” module is introduced and an empty list is created.
  • The “array()” function takes the list as an argument and converts the list into a Numpy array.
  • The “if” statement is used with the “size” attribute associated with the array to check whether the size of the array is “0”, which means it has no elements.
  • If the size is “0”, the array is empty and the corresponding statement comes into effect based on that.

Output

Conclusion

The “if/else” statement, “bool()” and “len()” functions, “not” operator, “NumPy” module, or comparison with an empty list can be used to check if a list contains no items in Python. This blog explored multiple ways to check whether the list contains any element or not in Python.

Share Button

Source: linuxhint.com

Leave a Reply