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:

  1. Using a for loop:
size = 5
empty_list = [None] * size
print(empty_list)
  1. Using list comprehension:
size = 5
empty_list = [None for i in range(size)]
print(empty_list)

Watch a course Python - The Practical Guide

  1. Using the * operator with the list() constructor:
size = 5
empty_list = list(None for i in range(size))
print(empty_list)
  1. Using * operator with []
size = 5
empty_list = [None] * size
print(empty_list)

Note that in all of the above methods, 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.