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

python— editable, runs on the server
  1. Using list comprehension:

Create a list of a specific size using list comprehension

python— editable, runs on the server

<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

python— editable, runs on the server

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.