
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
What is best way to check if a list is empty in Python?
The best way is to use not operator on list object. If list is empty it returns true, otherwise false.
>>> L1=[] >>> not L1 True >>> L1=[1,2] >>> not L1 False
Another method is to check if length of list is zero which means it is empty
>>> L1=[] >>> len(L1) 0 >>> L1=[1,2] >>> len(L1) 2
- Related Articles
- What is the best way to handle list empty exception in Python?
- How to check if a list is empty in Python?
- What is the most elegant way to check if the string is empty in Python?
- What is the correct way to check if String is empty in Java?
- How to check if a C# list is empty?
- Check if a list is not empty in MongoDB?
- What is the best way to check capacity in Java?
- Python - Check if dictionary is empty
- What is the best way to log a Python exception?
- Python program to check whether a list is empty or not?
- Python Program to check if the tuple is empty
- Python Pandas - Check if an interval is empty
- What is the best way to learn Python and Django?
- Golang to check if a slice is empty
- Python - Check if a list is contained in another list

Advertisements