How To Create An Empty List in Python with a Certain Size – Detailed Guide

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.

Basic Example

lst = [None] * 5

lst

Use this method ONLY when you want to store only Non-Reference Type objects.

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.

Output

[None, None, None, None, None]

This tutorial teaches you the different methods to create an empty list in python with a certain size.

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

Use this method when you want to create a list of non-reference types.

Code

The following code creates 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 Creating using * Operator

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
  • 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

The list comprehension offers a short syntax to create a list based on an existing list.

  • Use the list comprehension to create an empty list in Python.
  • Use the range() method to iterate a certain number of times and assign an empty list during each iteration.

Use this method when you want to create a list of reference types.

Code

The following code demonstrates how to create an empty list with 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 created using List Comprehension

You can assign a value at the specific index of the empty list using the append() method.

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

    [[1], [], [], [], []]

Using Numpy Array

NumPy library provides an empty() function to create an empty list.

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 objects of different types. For example, a single list can contain a String, a number, etc.

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 created
  • dtype – The type of object that needs to be stored in the list. Use dtype=object to store any type of object in the list.

Code

import numpy as np

lst = np.empty(5, dtype=object)

lst

Output

    array([None, None, None, None, None], dtype=object)

Initialize List of Size N with 0

To initialise a Python list of size N with value 0, 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 following 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 due to the reference nature.

lst = [[]] * 5

lst[0].append(1)

lst

Output

    [[1], [1], [1], [1], [1]]

The following 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], [], [], [], []]

Additional Resources

Leave a Comment