You cannot assign to a list like xs[i] = value, unless the list already is initialized with at least i+1 elements (because the first index is 0). Instead, use xs.append(value) to add elements to the end of the list. (Though you could use the assignment notation if you were using a dictionary instead of a list.)

Creating an empty list:

>>> xs = [None] * 10
>>> xs
[None, None, None, None, None, None, None, None, None, None]

Assigning a value to an existing element of the above list:

>>> xs[1] = 5
>>> xs
[None, 5, None, None, None, None, None, None, None, None]

Keep in mind that something like xs[15] = 5 would still fail, as our list has only 10 elements.

range(x) creates a list from [0, 1, 2, ... x-1]

# 2.X only. Use list(range(10)) in 3.X.
>>> xs = range(10)
>>> xs
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Using a function to create a list:

>>> def display():
...     xs = []
...     for i in range(9): # This is just to tell you how to create a list.
...         xs.append(i)
...     return xs
... 
>>> print display()
[0, 1, 2, 3, 4, 5, 6, 7, 8]

List comprehension (Using the squares because for range you don't need to do all this, you can just return range(0,9) ):

>>> def display():
...     return [x**2 for x in range(9)]
... 
>>> print display()
[0, 1, 4, 9, 16, 25, 36, 49, 64]
Answer from varunl on Stack Overflow
Top answer
1 of 16
1381

You cannot assign to a list like xs[i] = value, unless the list already is initialized with at least i+1 elements (because the first index is 0). Instead, use xs.append(value) to add elements to the end of the list. (Though you could use the assignment notation if you were using a dictionary instead of a list.)

Creating an empty list:

>>> xs = [None] * 10
>>> xs
[None, None, None, None, None, None, None, None, None, None]

Assigning a value to an existing element of the above list:

>>> xs[1] = 5
>>> xs
[None, 5, None, None, None, None, None, None, None, None]

Keep in mind that something like xs[15] = 5 would still fail, as our list has only 10 elements.

range(x) creates a list from [0, 1, 2, ... x-1]

# 2.X only. Use list(range(10)) in 3.X.
>>> xs = range(10)
>>> xs
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Using a function to create a list:

>>> def display():
...     xs = []
...     for i in range(9): # This is just to tell you how to create a list.
...         xs.append(i)
...     return xs
... 
>>> print display()
[0, 1, 2, 3, 4, 5, 6, 7, 8]

List comprehension (Using the squares because for range you don't need to do all this, you can just return range(0,9) ):

>>> def display():
...     return [x**2 for x in range(9)]
... 
>>> print display()
[0, 1, 4, 9, 16, 25, 36, 49, 64]
2 of 16
233

Try this instead:

lst = [None] * 10

The above will create a list of size 10, where each position is initialized to None. After that, you can add elements to it:

lst = [None] * 10
for i in range(10):
    lst[i] = i

Admittedly, that's not the Pythonic way to do things. Better do this:

lst = []
for i in range(10):
    lst.append(i)

Or even simpler, in Python 2.x you can do this to initialize a list with values from 0 to 9:

lst = range(10)

And in Python 3.x:

lst = list(range(10))
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-create-a-python-list-of-size-n
Python - Create List of Size n - GeeksforGeeks
July 23, 2025 - # Size of the list n = 5 # Creating a list of size n using list comprehension a = [0 for i in range(n)] # Print the list print(a)
Discussions

arrays - How do I get an empty list of any size in Python? - Stack Overflow
However, this is uncommon in python, unless you actually need it for low-level stuff. In most cases, you are better-off using an empty list or empty numpy array, as other answers suggest. ... Just to avoid a possible misunderstanding of "un-initialized": the numpy command np.empty(size) does return ... More on stackoverflow.com
🌐 stackoverflow.com
[deleted by user]
What even is an empty list with nonzero length? Those elements must be equal to something. More on reddit.com
🌐 r/learnpython
16
0
January 28, 2025
Why is it common in python to make an empty list or dict first?
In your case it’s completely useless however many times we run a loop and append to a list or add to a variable counter or many such things. This has to exist before we do so. I mean I thought a lot of list comprehension was supposed to help mitigate this very thing lol. for element in some_list: my_list.append(element*2) Will fail. More on reddit.com
🌐 r/Python
157
165
September 19, 2023
What is Python's list.append() method WORST Time Complexity? It can't be O(1), right?
Personally, I thought that lists worked as double LinkedLists, so insert time was O(1) But if it works as a dynamic array, time complexity should be amortized time, ie, close to O(1) but not quite More on reddit.com
🌐 r/learnpython
11
3
October 26, 2022
🌐
Career Karma
careerkarma.com › blog › python › python: initialize list of size n
Python: Initialize List of Size N: A Complete Guide | Career Karma
December 1, 2023 - To initialize a list of size n in Python, you can use multiplication syntax or a range() statement. On Career Karma, learn how to initialize empty lists of custom sizes.
🌐
Reddit
reddit.com › r/learnpython › [deleted by user]
[deleted by user] : r/learnpython
January 28, 2025 - Memory management? Nah, just let the garbage collector do it's job. But yeah, as others have said, you can't have an empty list with a size in python. It's either empty, or it isn't. C... You can have an empty list of n elements, because you have to explicitly allocate the memory.
🌐
JanBask Training
janbasktraining.com › community › python-python › python-empty-list-of-size-n
create an empty list in python with certain size | JanBask Training Community
July 27, 2021 - I want to create an empty list (or whatever is the best way) that can hold 10 elements.After that I want to assign values in that list, for example this is supposed to di
🌐
Sentry
sentry.io › sentry answers › python › create a list of a given size in python
Create a list of a given size in Python | Sentry
2 weeks ago - blank_list = [None] * 10 # will produce [None, None, None, None, None, None, None, None, None, None] None is a special Python object used to indicate null values. Because this list contains values at indices 0 to 9, we can access and reassign them.
🌐
EyeHunts
tutorial.eyehunts.com › home › python empty list of size n | example code
Python empty list of size n | Example code - EyeHunts
July 22, 2021 - You can create an empty list using the None Multiplication or Using the range() function in Python. The multiplication method is best if...
🌐
Note.nkmk.me
note.nkmk.me › home › python
Initialize a List of Any Size with Specific Values in Python | note.nkmk.me
August 20, 2023 - This article explains how to initialize a list of any size (number of elements) with specific values in Python. ... See the following article about initializing a NumPy array (numpy.ndarray). NumPy: Create an ndarray with all elements initialized with the same value · An empty list is created ...
Find elsewhere
🌐
YouTube
youtube.com › codelearn
python empty list of size n - YouTube
Download this code from https://codegive.com In Python, you can create an empty list of a specific size n using various approaches. Let's explore a couple of...
Published: December 21, 2023
Views: 6
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-initialize-empty-array-of-given-length
Python - Initialize empty array of given length - GeeksforGeeks
Let's see different Pythonic ways to create an empty list in Python with a certain size. One of the most simplest method to initialize the array is by *Operator. In this example, we are creating different types of empty using an asterisk (*) operator. ... # initializes all the 10 spaces with 0’s a = [0] * 10 print("Intitialising empty list with zeros: ", a) # initializes all the 10 spaces with None b = [None] * 10 print("Intitialising empty list of None: ", b) # initializes a 4 by 3 array matrix all with 0's c = [[0] * 4] * 3 print("Intitialising 2D empty list of zeros: ", c) # empty list which is not null, it's just empty.
Published: July 12, 2025
🌐
Techie Delight
techiedelight.com › home › python › create an empty list of specific size in python
Create an empty list of specific size in Python | Techie Delight
July 7, 2026 - This post will discuss how to create an empty list with a given size in Python. To assign any value to a list using the assignment operator at position i, a[i] = x, the list’s size should be at least i+1. Otherwise, it will raise an IndexError, as shown below: ... The solution is to create an empty list of None when list items are not known in advance.
🌐
Medium
medium.com › @ryan_forrester_ › how-to-create-empty-lists-in-python-d41cae5267e2
How to Create Empty Lists in Python | by ryan | Medium
October 26, 2024 - # Create empty list using the list constructor empty_list = list() print(empty_list) # Output: [] # Both methods create identical empty lists list1 = [] list2 = list() print(list1 == list2) # Output: True ... # Create a list of 5 None values size = 5 empty_list = [None] * size print(empty_list) # Output: [None, None, None, None, None] # Useful when you'll fill the list later empty_list[2] = "Hello" print(empty_list) # Output: [None, None, 'Hello', None, None]
🌐
Codemia
codemia.io › home › knowledge hub › create an empty list with certain size in python
Create an empty list with certain size in Python | Codemia
January 8, 2025 - The fastest way to create a list of a fixed size in Python is multiplication: [None] * n. This allocates the list in a single operation and fills every slot with None (or any other placeholder you choose).
🌐
DataCamp
datacamp.com › tutorial › python-empty-list
A Comprehensive Guide to Python Empty Lists | DataCamp
February 2, 2024 - Here’s an example of it in action: # Create an empty list using the list() function my_empty_list = list() Understanding how to manipulate empty lists is crucial for effective Python programming. Here are some key operations you will definitely need to use as you work with empty lists.
🌐
freeCodeCamp
freecodecamp.org › news › python-empty-list-tutorial-how-to-create-an-empty-list-in-python
Python Empty List Tutorial – How to Create an Empty List in Python
June 18, 2020 - You can create an empty list using an empty pair of square brackets [] or the type constructor list(), a built-in function that creates an empty list when no arguments are passed.
🌐
Purple Engineer
purpletutor.com › home › understanding code › mastering python empty lists creation usage optimization techniques
Python empty list basics and methods explained 🔧📈
December 27, 2025 - The boolean behavior of empty lists eliminates the need for explicit length checks or None comparisons in many scenarios. I frequently use this pattern in validation functions where I collect error messages in a list. If the list remains empty after validation, the boolean check cleanly indicates success without requiring additional variables or flags. Pre-allocating lists with specific sizes can provide significant performance benefits in scenarios where you know the expected data volume in advance.
🌐
Edureka Community
edureka.co › home › community › categories › python › create an empty list in python with certain size
Create an empty list in python with certain size | Edureka Community
August 2, 2018 - I want to create an empty list (or whatever is the best way) that can hold 10 elements. After ... just displays [] (empty). Can someone explain why?
🌐
EDUCBA
educba.com › home › software development › software development tutorials › python tutorial › python empty list
Python Empty List | How to Declare Empty List with Examples
April 13, 2023 - Explanation: In the above program, ... find the size of the declared variable which is a list, and it results in 0. So the output of the above program can be seen in the above screenshot. The list() constructor or built-in function of Python is used to create a list in Python. This is constructor is also used to create an empty list. Now let us an ...
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Scaler
scaler.com › home › topics › how to create an empty list in python?
How to Create an Empty List in Python? - Scaler Topics
June 27, 2022 - The list() constructor is used to create a list in python, and according to python documentation, if no arguments are given to the list() constructor, then the list() constructor creates an empty list in Python. ... As we can see in the output, the length of the list is 0, and the bool context ...