Check if a list is empty or not in Python (original) (raw)
Last Updated : 24 Oct, 2024
In article we will explore the different ways to check if a list is empty with simple examples. The simplest way to check if a list is empty is by using Python’s **not operator.
Using not operator
The **not operator is the simplest way to see if a list is empty. It returns **True if the list is empty and **False if it has any items.
Python `
a = [] if not a: print("The list is empty") else: print("The list is not empty")
`
**Explanation:
- **not **a returns **True because the list is empty.
- If there were items in the list, **not a would be **False.
Table of Content
- Using len()
- Using Boolean Evaluation Directly
- Frequently Asked Questions to Check List is Empty or Not
Using len()
We can also use **len() function to see if a list is empty by checking if its length is zero.
Python `
a = [] if len(a) == 0: print("The list is empty") else: print("The list is not empty")
`
**Explanation: len(a) returns **0 for an empty list.
Using Boolean Evaluation Directly
Lists can be directly evaluated in conditions. **Python considers an empty list as False.
Python `
a = [] if a: print("The list is not empty") else: print("The list is empty")
`
**Explanation: An empty list evaluates as **False in the if condition, so it prints “The list is empty”.
Similar Reads
- Python - Check if a list is empty or not In article we will explore the different ways to check if a list is empty with simple examples. The simplest way to check if a list is empty is by using Python's not operator. Using not operatorThe not operator is the simplest way to see if a list is empty. It returns True if the list is empty and F 2 min read
- Check if a List is Sorted or not - Python We are given a list of numbers and our task is to check whether the list is sorted in increasing or decreasing order. For example, if the input is [1, 2, 3, 4], the output should be True, but for [3, 1, 2], it should be False. Using all()all() function checks if every pair of consecutive elements in 3 min read
- Python | Check if list is Matrix Sometimes, while working with Python lists, we can have a problem in which we need to check for a Matrix. This type of problem can have a possible application in Data Science domain due to extensive usage of Matrix. Let's discuss a technique and shorthand which can be used to perform this task.Metho 3 min read
- Check if element exists in list in Python In this article, we will explore various methods to check if element exists in list in Python. The simplest way to check for the presence of an element in a list is using the in Keyword. Example: [GFGTABS] Python a = [10, 20, 30, 40, 50] # Check if 30 exists in the list if 30 in a: print("Eleme 3 min read
- How to check if a deque is empty in Python? In this article, we are going to know how to check if a deque is empty in Python or not. Python collection module provides various types of data structures in Python which include the deque data structure which we used in this article. This data structure can be used as a queue and stack both becaus 3 min read
- Declare an empty List in Python Declaring an empty list in Python creates a list with no elements, ready to store data dynamically. We can initialize it using [] or list() and later add elements as needed. Using Square Brackets []We can create an empty list in Python by just placing the sequence inside the square brackets[]. To de 3 min read
- How to check if an object is iterable in Python? In simple words, any object that could be looped over is iterable. For instance, a list object is iterable and so is an str object. The list numbers and string names are iterables because we are able to loop over them (using a for-loop in this case). In this article, we are going to see how to check 3 min read
- Python | Check if one list is subset of other Checking if one list is a subset of another is a common task when working with data collection. In this article, we will see different ways to perform this check. Using set.issubset() Method issubset() method is the best way to check if all elements of one list exist in another. It Converts lists to 2 min read
- Check if the image is empty using Python - OpenCV Prerequisite: Basics of OpenCV OpenCV (Open Source Computer Vision) is a computer vision library that contains various functions to perform operations on pictures or videos. It was originally developed by Intel but was later maintained by Willow Garage and is now maintained by Itseez. This library i 2 min read
- IndexError: pop from Empty List in Python The IndexError: pop from an empty list is a common issue in Python, occurring when an attempt is made to use the pop() method on a list that has no elements. This article explores the nature of this error, provides a clear example of its occurrence, and offers three practical solutions to handle it 3 min read