To create an empty list in Python, you can initialize a list with no items with open and closed square brackets.
empty_list = []
In Python, lists are a collection of objects which are unordered. When working with lists, it can be useful to be able to easily create an empty list, or a list with no items.
To create an empty list in Python, you can initialize a list with no items with open and closed square brackets.
Below is a simple example showing you how to create an empty list in Python.
empty_list = []
Properties of Empty Lists in Python
There are a few properties that empty lists have in Python.
First, empty lists have length 0 and therefore are equal to False when converted to a boolean value.
Empty lists have all of the methods available to them like regular lists and typically you might initialize an empty list and then add items one by one as shown in the following code.
empty_list = []
empty_list.append(1)
empty_list.append(2)
empty_list.append(3)
print(empty_list)
#Output:
[1,2,3]
How to Check if List is Empty in Python
If you want to check if a list is empty in Python, you just need to check for certain conditions.
We can easily check if a list is empty in Python. An empty list has length 0, and is equal to False, so to check if a list is empty, we can just check one of these conditions.
Below are three ways you can check if a list is an empty list in Python.
empty_list = []
#length check
if len(empty_list) == 0:
print("List is empty!")
#if statement check
if empty_list:
print("List is empty!")
#comparing to empty list
if empty_list == []:
print("List is empty!")
Hopefully this article has been useful for you to learn how to create an empty list in Python.