W3docs

Create an empty list with certain size in Python

You can create an empty list of a certain size in Python using the following methods:

You can create a list of a specific size in Python using the following methods:

  1. Using the multiplication operator:

Create a list of a specific size using the multiplication operator

size = 5
empty_list = [None] * size
print(empty_list)
  1. Using list comprehension:

Create a list of a specific size using list comprehension

size = 5
empty_list = [None for i in range(size)]
print(empty_list)

<div class="alert alert-info flex not-prose"> Watch a course <span class="hidden md:block">Watch a video course </span> Python - The Practical Guide</div>

  1. Using a generator expression with list():

Create a list of a specific size using a generator expression

size = 5
empty_list = list(None for i in range(size))
print(empty_list)

Note that in the methods above, the variable size represents the desired size of the list, and the value None can be replaced with any other value if you want the list to be initialized with a specific value. The multiplication operator ([None] * size) is generally preferred for its simplicity and performance.