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))
🌐
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. Create an empty list Initialize a list of any size with specific values Notes on initial ...
Discussions

python - How do I check if a list is empty? - Stack Overflow
Comparing the length of an array to 0 to determine emptiness is standard practice in c and almost all c influenced languages (c++, java, c#, etc). 2019-04-14T04:52:39.347Z+00:00 ... Coming from a language that claims to be some sort of poetry, this mechanism is pure garbage. Semantically, being empty is very different to not being 2020-04-04T14:03:17.647Z+00:00 ... The Pythonic way to do it is from the PEP 8 style guide. For sequences, (strings, lists... More on stackoverflow.com
🌐 stackoverflow.com
How can I make an empty list of size 4 in python? - Stack Overflow
Useful reference for this topic: ...to/python/python-initialize-empty-list/ ... Sign up to request clarification or add additional context in comments. ... You must not, as these are mutually exclusive requirements: to have list which is empty (i.e. has size 0) and has size 4 ... You can't make an empty list of length n because ... 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
python - Does empty list have a length? - Stack Overflow
I am writing a function that takes two arrays a and b of length n storint int values, and returns the dot product of a and b. That is, it returns an array c of length n such that c[i]=a[i]*b[i],for... More on stackoverflow.com
🌐 stackoverflow.com
🌐
DataCamp
datacamp.com › tutorial › python-empty-list
A Comprehensive Guide to Python Empty Lists | DataCamp
February 2, 2024 - For those in a hurry, creating an empty list in Python is as straightforward as using square brackets []. This concise method is not only easy to remember but also efficient in initializing a list for further operations.
🌐
Quora
quora.com › What-is-the-length-of-an-empty-list-in-Python
What is the length of an empty list in Python? - Quora
Answer (1 of 4): we use the len() to check if a list is empty, this function returns the length of the argument passed. And given the length of an empty list is 0 it can be used to check if a list is empty in Python.
🌐
Python Morsels
pythonmorsels.com › checking-for-an-empty-list-in-python
Checking for an empty list in Python - Python Morsels
August 20, 2024 - Or if we wanted to check for non-empty lists, we could make sure that the length is greater than 0: >>> if len(numbers) > 0: ... print("The list is NOT empty.") ... But this is actually not the most typical way to check for an empty list in Python. Many Python users prefer to check for an empty list by evaluating the list's truthiness.
🌐
PythonHow
pythonhow.com › how › check-if-a-list-is-empty
Here is how to check if a list is empty in Python
To check if a list is empty in Python, you can use the len() function to check the length of the list.
🌐
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.
🌐
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 - Hence a list can contain several data types like Integers, Strings, and Objects. Empty list is a list having no elements, therefore the length of an empty list is 0, and the bool context of an empty list is false.
Find elsewhere
🌐
W3docs
w3docs.com › python
Create an empty list with certain size in Python
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.
🌐
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?
🌐
Medium
medium.com › swlh › efficiently-checking-for-an-empty-list-in-python-76b76099fbd3
Efficiently Checking for an Empty List in Python | by Frank Scholl | The Startup | Medium
November 22, 2019 - That means whatever is done behind the scenes has native support in C for determining if the list is empty. If you look into the source code of Python, you’ll find that the comparison is looking at the C object’s size property and returning True if it is greater than 0 or False if it isn’t. Thus, it is a streamlined implementation of the explicit length method implemented by the list object in C.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-initialize-empty-array-of-given-length
Python - Initialize empty array of given length - GeeksforGeeks
In this example, we are using Python List comprehension for 1D and 2D empty arrays. Using list comprehension like [[0] * 4 for i in range(3)] creates independent lists for each row. Each iteration in the list comprehension creates a new list object, which ensures that changes to one row will not affect others. ... # initialize the spaces with 0’s with # the help of list comprehensions a = [0 for x in range(10)] print(a) b = [[0] * 4 for i in range(3)] print(b)
Published   July 12, 2025
🌐
Reddit
reddit.com › r/learnpython › [deleted by user]
Why can't I specify an empty list of size N using `*`?
January 28, 2025 - In Python, empty list is the list of length 0 by definition.
🌐
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
🌐
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
October 15, 2023 - I’ve tried using this code to create a list of length 10: numbers = list() for i in range(0, 10): numbers[i] = i ... In Python, lists do not have fixed sizes, and subscript notation (list[i]) can only be used to access and assign existing elements. This code creates an empty list (size 0) and then attempts to access positions 0 to 9, which do not exist.
🌐
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 - When you print an empty list, it just prints empty square brackets. To check if the list is empty or not you can use the len() function which gives the number of elements in the list. Let’s check the size of the list.
🌐
Replit
replit.com › home › discover › how to create an empty list in python
How to create an empty list in Python
February 6, 2026 - The SimpleBuffer class uses an empty list to manage a collection with a fixed capacity. When you create an instance, like SimpleBuffer(3), you set its maximum size. The add() method uses append() to add a new item to the end of the list. If the list’s length exceeds the defined max_size, the code calls pop(0) to remove the element at the very first index.