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:
- Using a for loop:
size = 5
empty_list = [None] * size
print(empty_list)
- Using list comprehension:
size = 5
empty_list = [None for i in range(size)]
print(empty_list)
- Using the
*
operator with thelist()
constructor:
size = 5
empty_list = list(None for i in range(size))
print(empty_list)
- 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.