How to Create an Empty List in Python?

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

What is an Empty List in Python?

Before moving to Empty List, let's quickly recap List in Python.

List in Python is just like arrays but with a major advantage that a List can contain heterogeneous items, making a list a great Python tool. Hence a list can contain several data types like Integers, Strings, and Objects.

Now let's see what is an Empty List in Python, as the name indicates an empty list is a list having no elements. But if there are no elements in the list, then how can we use the list?

An empty list is declared at the beginning of the program, and if we need to operate on the list, we can insert the element using the append() method in Python.

Now let's see how we can create an Empty List in Python.

How to Create an Empty List in Python?

An empty list in Python can be created in two ways, either by using square brackets [] or by using the [list() constructor. Let's look at these ways, and we will also insert elements after creating an empty list in Python.

Create an Empty List in Python Using []

We can create an empty list in Python using [] by assigning square brackets to a variable.

But how do we verify if data is an empty list or not?
As the name implies empty list must not have any element therefore, the length of an empty list must be zero, plus an empty list in python results in false values. Hence if bool(data) is printed, it should evaluate as false. Let's verify whether data is an empty list or not.

Code:

Output:

Explanation:

In the above example, the data list passed in both conditions therefore, data is an empty list in Python.

Now we have created an empty list in Python; let's add some items to the list using the append() method in Python.

Code:

Output:

Explanation:

The above example shows how we can add the items in an empty list (which is created using []) with the help of the append() method in python.

Create an Empty List in Python Using list()

Another way to create an empty list in Python is using the list() constructor. The list() constructor is used to create a list in python, and according to python documentation, if no arguments are given to the list() constructor, then the list() constructor creates an empty list in Python.

Code:

Output:

Explanation:

As we can see in the output, the length of the list is 0, and the bool context of the list is evaluated as false. Hence data is an empty list.

Now let's add some elements to the list using Python's append() method.

Code:

Output:

Explanation:

The above example shows how we can add the items in an empty list using python's append() method. Use this Python Compiler to compile your code.

Conclusion

  • List is a data type that is used to store heterogeneous items. Hence a list can contain several data types like Integers, Strings, and Objects.
  • Empty list is a list having no elements, therefore the length of an empty list is 0, and the bool context of an empty list is false.
  • Generally, an empty list is declared at the beginning of the program, and the append() method is used to insert elements into the list.

See Also: