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))
๐ŸŒ
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. Here again, there are two techniques that can be used. The first method is base...
Discussions

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
How can the length of a list be equal to 0?
The length of a list is 0 when the list is empty. The length of a list will never be less than 0, because a list can never be "less than empty." The problem is, [while len(new_numbers) <= number of values I want] did not come out as I would expect. Well, no. Because the body of the while loop runs every time the test is true, which means that when len(new_ numbers) equals the number of values you want, you run the loop body one more time, which appends an extra value onto the list. If I type in the codebelow it returns a list with one value in it. How is that possible? Because the list is only empty once, so the loop body runs once. Once it runs it's no longer the case that the length of the list is equal to or less than zero. Overall I think the issue seems to be that you don't remember from school what <, >, <=, and >= mean. More on reddit.com
๐ŸŒ r/learnpython
4
1
October 4, 2019
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
I keep getting : "cannot .pop from empty list" error (I'm a serious noob btw)
In your for loop your are emptying your array2, but you don't change array1 or y. Thus, once array2 is completely empty y is still 0 and array1 is still its original length. Therefore the while loop continues for a second iteration. Now, when you enter the for loop again, array2 is already empty, but you're still trying to remove elements, thus you get an error. Anyway, have a look at zip() this will make your life a lot easier ;) https://realpython.com/python-zip-function/#traversing-lists-in-parallel Btw, also have a look at the raise keyword: https://www.w3schools.com/python/ref_keyword_raise.asp And also at append(): https://www.w3schools.com/python/ref_list_append.asp More on reddit.com
๐ŸŒ r/learnpython
9
1
June 16, 2021
๐ŸŒ
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
๐ŸŒ
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.
๐ŸŒ
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 - 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.
๐ŸŒ
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 - Therefore, in most cases, it is not necessary to initialize the list in advance. If you want to initialize a list with a specific size where all elements have the same value, you can use the * operator as shown below.
๐ŸŒ
Python Morsels
pythonmorsels.com โ€บ checking-for-an-empty-list-in-python
Checking for an empty list in Python - Python Morsels
July 18, 2026 - 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.
Find elsewhere
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ [deleted by user]
[deleted by user] : r/learnpython
January 28, 2025 - In Python, empty list is the list of length 0 by definition. You can't define an empty list of any size other than 0 by any syntax. ... Multiplying any number by 0 gives you 0. So when you've got nothing and you multiply it by something you still have nothing.
๐ŸŒ
DataCamp
datacamp.com โ€บ tutorial โ€บ python-empty-list
A Comprehensive Guide to Python Empty Lists | DataCamp
February 2, 2024 - The use of square brackets [] to create an empty list is a testament to Python's design philosophy, emphasizing simplicity and readability. It represents a clean, intuitive starting point for list operations. Hereโ€™s a reminder of how to initialize an empty list using []: # Initializing an empty list with square brackets my_empty_list = []
๐ŸŒ
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). It is the standard approach when you know the size upfront and plan to assign values ...
๐ŸŒ
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?
๐ŸŒ
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 most Pythonic way to check if a list is empty is using a boolean expression like if not my_list, as empty lists evaluate to False. You can also use len(my_list) == 0 for explicit length checking.
๐ŸŒ
PythonHow
pythonhow.com โ€บ how โ€บ check-if-a-list-is-empty
Here is how to check if a list is empty in Python
New: Practice Python, JavaScript ... Output ยท The list is empty. Explanation ยท To check if a list is empty in Python, you can use the len() function to check the length of the list....
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ How-to-create-an-empty-list-in-Python
How to create an empty list in Python?
April 17, 2025 - my_list = [] print("Empty list:", my_list) print("List length:", len(my_list)) ... In Python, we have another way to create an empty list using the built-in list() constructor. This constructor is commonly used for typecasting (converting one ...
๐ŸŒ
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 - We can create an empty list in Python using [] by assigning square brackets to a variable. But how do we verify if data is an empty list or not? As the name implies empty list must not have any element therefore, the length of an empty list must be zero, plus an empty list in python results ...
๐ŸŒ
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 - Thus, it is a streamlined implementation of the explicit length method implemented by the list object in C. ... By default, an object is considered true unless its class defines either a __bool__() method that returns False or a __len__() method that returns zero, when called with the object. ... For sequences, (strings, lists, tuples), use the fact that empty sequences are false.Yes: if not seq: if seq: No: if len(seq): if not len(seq): Empirically, the best way to check for an empty list in Python is by implicit boolean conversion.
๐ŸŒ
The Renegade Coder
therenegadecoder.com โ€บ code โ€บ how-to-check-if-a-list-is-empty-in-python
How to Check if a List is Empty in Python: Type Flexibility and More โ€“ The Renegade Coder
May 21, 2024 - As always, in this section, we do a little recap of the solutions weโ€™ve shared above: my_list = list() # Check if a list is empty by its length if len(my_list) == 0: pass # the list is empty # Check if a list is empty by direct comparison ...
๐ŸŒ
Tutorial Gateway
tutorialgateway.org โ€บ python-list-length
Python list length
April 19, 2026 - To demonstrate the same, we declared a list of integers, Python strings, and floating-point numbers. As you can see from the result, the len() function has no issues in finding the length of a list with mixed data ...