Alex Rivera | Logout

Create an empty list with certain size in Python

Asked 2012-05-23T00:38:24.613
932

How do I create an empty list that can hold 10 elements?

After that, I want to assign values in that list. For example:

xs = list()
for i in range(0, 9):
   xs[i] = i

However, that gives IndexError: list assignment index out of range. Why?

Edit
Report

2 Answers

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]
answered 2012-05-23T00:44:12.223
11

How do I create an empty list that can hold 10 elements?

All lists can hold as many elements as you like, subject only to the limit of available memory. The only "size" of a list that matters is the number of elements currently in it.

However, that gives IndexError: list assignment index out of range. Why?

The first time through the loop, i is equal to 0. Thus, we attempt xs[0] = 0. This does not work because there are currently 0 elements in the list, so 0 is not a valid index.

We cannot use indexing to write list elements that don't already exist - we can only overwrite existing ones. Instead, we should use the .append method:

xs = list();
for i in range(0, 9):
   xs.append(i)

The next problem you will note is that your list will actually have only 9 elements, because the end point is skipped by the range function. (As side notes: [] works just as well as list(), the semicolon is unnecessary, and only one parameter is needed for range if you're starting from 0.) Addressing those issues gives:

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

However, this is still missing the mark - range is not some magical keyword that's part of the language the way for (or, say, def) is.

In 2.x, range is a function, which directly returns the list that we already wanted:

xs = range(10) # 2.x specific!
# In 3.x, we don't get a list; we can do a lot of things with the
# result, but we can't e.g. append or replace elements.

In 3.x, range is a cleverly designed class, and ra

answered 2012-05-23T00:50:07.423

Your Answer