How to make an empty list in python

In this post, I will be sharing how to make an empty list in Python. Lists are similar to arrays, declared in other programming languages. Lists in Python need not be homogeneous i.e. a single list may contain different data types such as Strings, Integers, or Objects.

Read Also: list of lists in Python

There are two ways to make an empty list in Python:
1. Using square brackets [] [Recommended]
2. Using list() constructor

Make an empty list in Python


1. Using square brackets []


It is easy to create an empty list in Python. Just use empty pair of square brackets as shown below.
 var = [] // creates an empty list
print("value of var: ",var)
print("length of var:", len(var))
print("Type of var:", type(var))
var.append(10)
var.append(20)
print("values of var: ",var)

Output
value of var: []
length of var: 0
Type of var: <class 'list'>
values of var: [10, 20]


2. Using list() constructor


The list() constructor creates a new empty list object. Below, we have not passed any arguments to the list(), as a result, we get an empty list.
 var = list()
print("value of var: ",var)
print("length of var:", len(var))
print("Type of var:", type(var))
var.append(30)
var.append(40)
print("values of var: ",var)

Output
value of var: []
length of var: 0
Type of var: <class 'list'>
values of var: [30, 40]


Which is more faster [] or list()


[] is faster than list() because list() has to perform the following intermediate steps while square brackets does not:
a. symbol lookup
b. calling the function
c. check if there were iterable arguments passed. If yes, then it will create a list with elements from it as shown in the above example.

That's all for today. Please mention in the comments in case you have any questions related to how to make an empty list in Python.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry