Python lists are used to store multiple items in one object.
You can create an empty list in Python with a certain size using the lst = [None] * 10 statement.
In this tutorial, you’ll learn how to create an empty list in python with a certain size.
If You’re in Hurry…
To create an empty list in python, you can store the None
value that acts as a placeholder for missing values. None value is used to denote the absence of an actual value.
Use the below statement to create an empty list with the size 5
.
Use this method ONLY when you want to store only Non-Reference Type objects.
Code
lst = [None] * 5
lst
Output
[None, None, None, None, None]
Once you’ve created a list, you can add items to the list using the append() method or the extend() method.
If You Want to Understand Details, Read on…
Different methods are available to create an empty list with a certain size python. Let us look at each one in detail.
Table of Contents
Using * operator
You can use the None
value and the *
operator to create an empty list with an n
size. The n
denotes the size of the empty list.
None value is used to denote the absence of an actual value.
You can use this method when you want to create a list of non-reference types.
Code
The below code is used to create a Python list with a size of 5
.
lst = [None] * 5
lst
Output
When you print the empty list, you’ll see the 5 None
values, which denote the missing value.
[None, None, None, None, None]
Assigning Value to The list
This section demonstrates what happens when you create an empty list using the *
operator for the reference type objects.
Create an empty list with the []
. This is usually a list of lists.
Then assign a value to the first index position using the append method.
lst = [[]] * 5
lst[0].append(1)
lst
When you print the list, you’ll see that the value is assigned to every index. This can be avoided when you use list comprehension to create an empty list for reference types. (explained in next section).
Output
[[1], [1], [1], [1], [1]]
Using List Comprehension
You can use list comprehension to create an empty list in Python.
The list comprehension offers a short syntax to create a list based on an existing list.
You can use the range() method to iterate a certain number of times and assign an empty list during each iteration.
You can use this method when you want to create a list of reference types.
Code
The code below demonstrates how to create an empty list with the size 5
using the list comprehension method.
lst = [[] for i in range(5)]
lst
Output
When you print the empty list, you’ll see the below output.
[[], [], [], [], []]
Assigning Value to The List
You can assign a value at the specific index of the empty list using the append()
method.
Code
The code below demonstrates assigning a value to the first index position 0
of an empty list.
lst = [[] for i in range(5)]
lst[0].append(1)
lst
Since the empty list is created using the list comprehension method, the value will be assigned ONLY at the specific index.
Output
When you print the empty list, you’ll see the below output.
[[1], [], [], [], []]
Using Numpy Array
NumPy library provides an empty() function to create an empty list.
You can use this method when you want to store any specific object type in the empty list. By default, a Python list allows you to store the objects of different types. For example, a single list can contain a String, a number, etc.
Installing Numpy
You can use the below pip
command to install numpy.
Prefix the %
operator to install it directly from the Jupyter notebook.
pip install numpy
Creating An Empty List Using Numpy
You can use the empty()
method in the NumPy library to create an empty list.
It accepts two parameters.
Size
– Size of the empty list to be createddtype
– The type of object that needs to be stored in the list. Usedtype=object
to store any type of object in the list.
Code
import numpy as np
lst = np.empty(5, dtype=object)
lst
Output
When you print the list, you’ll see the list and its dtype.
array([None, None, None, None, None], dtype=object)
This is how you can use the NumPy library to create an empty list with a specific size.
Initialize List of Size N with 0
To initialise a Python list of size N with value 0
, you can use the below code.
lst = [0] * 10
lst
Output
Each index of the list will contain the value 0.
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Initialize List of Size for Reference Types
This section demonstrates how the *
operator and the list comprehension behave differently for the reference type objects.
The below code demonstrates what happens when you use the * operator for the reference type. Assigning a value to a specific index also modifies the values at the other indexes. This is to the reference nature.
lst = [[]] * 5
lst[0].append(1)
lst
Output
[[1], [1], [1], [1], [1]]
The below code demonstrates what happens when you use the list comprehension for the reference type. Assigning a value to a specific index doesn’t modify the values at the other indexes as well.
lst = [[] for x in range(5)]
lst[0].append(1)
lst
Output
[[1], [], [], [], []]
Conclusion
You’ve learned how to create an empty list in python with a certain size.
You’ve also learned when to use the *
operator, when to use the list comprehension method and when to use the Numpy
library to create an empty list with a specific size.
Additionally, you’ve learned how to make a list with N elements with a default value of 0.