Updated April 13, 2023
Introduction to Python Empty List
In this article, we will discuss the concept of how to create an empty list in Python. In Python, the list is similar to an array that is a data structure that is an ordered sequence of defined elements within the square brackets. In this, we will see what an empty list is and how to declare it in Python. In python, an empty list is defined as the list with no elements or items on the list. To define an empty list in Python, there are two ways to do this, and they are done either by using square bracket [] or by using a list constructor such as list().
How to Declare Empty List?
In this article, an empty list is created by just declaring square brackets [] with no elements within the assignment statement’s brackets. It can also be created by using a built-in function list(), or you can say it as a constructor. Let us these two ways in detail with examples below, which demonstrates how to create an empty list in Python.
1. Using square brackets []
In this method, an empty list can be created using square brackets [] by placing the sequences of elements inside the square brackets. This can be done by just assigning a variable with square brackets [].
Example
Code:
print("The below program demonstrates how to declare empty list:")
print("\n")
print("To declare the empty list using square brackets as follows:")
a = []
print("The Value of a is as follows:")
print(a)
print("\n")
print("The Type of a is:")
print(type(a))
print("\n")
print("The Size of a is as follows:")
print(len(a))
Output:
Explanation: In the above program, we can see that variable “a” with assigning it with blank square brackets []. So we can see that to declare a variable “a” as an empty list, we have assigned it to just an empty square bracket [], and we can see what type is variable “a” it results in the type as “list” and we also find the size of the declared variable which is a list, and it results in 0. So the output of the above program can be seen in the above screenshot.
2. Using a list() function or list() constructor
The list() constructor or built-in function of Python is used to create a list in Python. This is constructor is also used to create an empty list. Now let us an example and syntax of how the list constructor can be used for creating an empty list.
Syntax:
list(iterable)
where the parameter is iterable, which can be either list, set, tuples, dictionary, etc. But for creating an empty list, there is no parameter passed; then it returns an empty list. If there are any parameters passed, then it returns the list of those elements passed to the argument.
Example #1
Now let us see an example of creating an empty list using the list() function or constructor.
Code:
print("The below program demonstrates the creation of empty list using list function or constructor:")
print("\n")
print("Creation of empty list using list constructor:")
a = list()
print("The Values of a is as follows:")
print(a)
print("\n")
print("The Type of a is :")
print(type(a))
print("\n")
print("The Size of a is:")
print(len(a))
Output:
Explanation: In the above program, we can see that we have declared a variable “a”, and we have assigned it to a list() function with no arguments passed to this function. Then we are printing the type of variable “a” to know if it is a list, and the size of the list also is printed as “0”, which indicates an empty list has been created.
Example #2
Now let us see how to add elements to the empty list in Python. There are 3 ways to do this, such as by using append(), insert() and extend() functions in Python. Let us consider the example below of adding elements to the empty list.
Code:
print("Program for adding elements to the empty list")
print("\n")
l = []
print("Adding elements using append function is as follows:")
l.append("Python")
l.append("Perl")
print(l)
print("\n")
print("Adding elements using insert function at position 0 and 2 is as follows:")
l.insert(0, "PHP")
l.insert(2, "Lua")
print(l)
print("\n")
print("Adding elements using extend function is as follows:")
l.extend(("JavaScript", "ActionScript"))
print(l)
Output:
Explanation: In the above program, we saw we first created an empty list by using square brackets with the variable as “l” then we have added elements by using the append() function, insert() function, which added the element at 0th and 2nd position of the list as mentioned in the program and last we added the elements using extend() function which adds the elements at the end of the created list.
In Python, as we saw, there are two ways to create an empty list, such as using square brackets [] and using the list constructor or list() function. But the main difference between these two ways is that using square brackets [] is more preferable to using list() function or constructor because using square brackets is faster than using list() function as a list() requires symbol lookup, it might take a list() as an extra function call as it might take it as constructor call and if it is constructor call, then it will check for iterable inside the argument if no arguments then only it creates an empty list. Whereas square brackets [] is just a literal which will always give the same output, it will always return an empty list.
Conclusion
In this article, we saw how to create an empty list in Python. We saw there are two ways, such as using square brackets [] and using list() constructor or list() function to create an empty list with examples. We also saw between these two ways, which is faster and why. We also saw how to add elements to the empty list in Python by using append(), insert(), and extend() function. Hence we conclude that creating an empty list in Python is very simple, and using square brackets [] is faster than using list() constructor or list() function.
Recommended Articles
We hope that this EDUCBA information on “Python Empty List” was beneficial to you. You can view EDUCBA’s recommended articles for more information.