Create Empty List with Certain Size in Python (2 Examples)
In Python programming, creating an empty list generally refers to a list that contains no elements. However, there are situations where you may want to establish a list wwithout any actual elements, but reserving a given number of slots or placeholders for future data. On this page you’ll learn how to initialize such empty lists in Python.
The article will contain the following contents:
If you want to learn more about these topics, keep reading!
Example Data
We’ll use the following predefined list length in this tutorial.
size = 5 # define preferred size
Our list size is defined, now we can jump into the example creating an empty list in this given size.
Example 1: Initialize Empty List with Given Size using List Multiplication
You can create an empty list with a specified size using list multiplication. All you need is to create a list with None
value as a placeholder then multiply it with the preferred size.
empty_list = [None] * size print(empty_list) # [None, None, None, None, None]
The previously Python console shows that an empty list with 5 placeholders has been created successfully. Needless to say, you can also use empty string ""
number zero 0
, or something else as placeholder. It is up to your choice.
Let’s now check the other option!
Example 2: Initialize Empty List with Given Size using List Comprehension
In this example, we will utilize list comprehension to create a list with placeholders. See the script below.
empty_list = [None for i in range(size)] print(empty_list) # [None, None, None, None, None]
The list comprehension has populated a list with None
values, repeating as many times as the specified size
. If you’re familiar with list comprehensions, it’s the method to go!
Video, Further Resources & Summary
Do you want to learn more about the initialization of empty lists in given sizes? Then you may have a look at the following video tutorial on my YouTube channel. In the video, I’m explaining the Python programming codes of this tutorial.
The YouTube video will be added soon.
In addition, you may read the other articles on my website:
- Replace Element at Certain Index in List in Python
- Check if List is Empty in Python
- Create 2D List in Python
- Create Empty 2D List in Python
- Introduction to Python Programming
In this Python tutorial you have learned how to create an empty list with place holders. Please let me know in the comments below, if you have further questions.
This page was created in collaboration with Cansu Kebabci. Have a look at Cansu’s author page to get more information about her professional background, a list of all his tutorials, as well as an overview on her other tasks on Statistics Globe.