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
1380

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
232

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)
🌐
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
A more Pythonic way to create the list in the code above would be to use the range function directly in the list creation: ... empty_list = list() # list with zero elements other_empty_list = [] # list with zero elements, alternate syntax short_list = ["Hello world!"] longer_list = [1, 2, 3] Because lists do not have fixed sizes, elements can be added to the end of any existing list using the list.append method:
🌐
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 as follows.
🌐
W3docs
w3docs.com › python
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) Try it Yourself » · Copy · Using list comprehension: size = 5 empty_list = [None ...
🌐
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 - This code creates a list object with eight values. Each of the values in the list will be equal to None. ... The value None could be substituted with any default value that you want. For instance, you could make each default value an empty string (“”), the number zero (0), or something else.
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-initialize-empty-array-of-given-length
Python - Initialize empty array of given length - GeeksforGeeks
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 ...
Published   July 12, 2025
🌐
Reddit
reddit.com › r/learnpython › [deleted by user]
Why can't I specify an empty list of size N using `*`?
April 13, 2024 - 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. ... What's an "empty list of size N"? An empty list has size 0 by definition. Otherwise it wouldn't be an empty list. And []*N does create an empty list (assuming ...
Find elsewhere
🌐
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 ...
🌐
Codemia
codemia.io › knowledge-hub › path › create_an_empty_list_with_certain_size_in_python
Create an empty list with certain size in Python
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises
🌐
Quora
quora.com › How-do-I-create-an-empty-list-in-Python
How to create an empty list in Python - Quora
Answer (1 of 6): Initialize empty list in Python. 1. arr1 = [] 2. arr2 = list() Note : arr1 is same as arr2 Want to learn Python? Follow my Instagram page
🌐
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.
🌐
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
🌐
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?
🌐
Finxter
blog.finxter.com › home › learn python blog › how to create an empty list in python?
How to Create an Empty List in Python? - Be on the Right Side of Change
October 14, 2022 - Python is a dynamic language so there is no concept of a “list of type X”. Instead of creating a list of a fixed type, simply create an empty list using [] or list() and assign it to a variable such as my_list.
🌐
GeeksforGeeks
geeksforgeeks.org › python › declare-an-empty-list-in-python
Declare an Empty List in Python - GeeksforGeeks
July 12, 2025 - We can initialize it using [] or list() and later add elements as needed. We can create an empty list in Python by just placing the sequence inside the square brackets[]. To declare an empty list just assign a variable with square brackets.
🌐
TutorialsPoint
tutorialspoint.com › how-to-create-a-list-of-n-lists-in-python
How to Create a List of N-Lists in Python?
This approach provides a concise and efficient way to create the desired list of N-lists. ... Import the itertools module. Define the dimensions of the list of lists. Use the itertools.repeat() function to repeat an empty list n times.
🌐
Delft Stack
delftstack.com › home › howto › python › python initialize empty list
How to Initialize an Empty List in Python | Delft Stack
March 4, 2025 - You can create an empty list in Python using either the list() constructor or by using square brackets []. Can I initialize an empty list with a specific size? Yes, you can initialize a list with a specific size by using list comprehension, ...
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › how to create empty list in python
How to Create Empty List in Python - Spark By {Examples}
May 31, 2024 - # Empty List reasltime usage list1 ... article, you have learned how to create an empty list in Python by using the empty square brackets [] and list() function....