# Create a Fixed-Size List in Python with Examples

> Initialize fixed-size Python lists with multiplication syntax or use a deque with maxlen for bounded collections that prevent index assignment errors

**URL:** https://sentry.io/answers/create-a-list-of-a-given-size-in-python/

---

## The Problem

In Python, how do I create a list of specific sizes and assign elements to it? I've tried using this code to create a list of length 10:

```python
numbers = list()
for i in range(0, 10):
   numbers[i] = i
```

It fails with the following error:

```
IndexError: list assignment out of range
```

Why does this happen and how do I fix it?

## The Solution

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.

A more Pythonic way to create the list in the code above would be to use the `range` function directly in the list creation:

```python
numbers = list(range(0, 10))
```

Additional examples of list creation code are:

```python
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:

```python
empty_list.append(1)  # will now be [1]
short_list.append("It's me!")  # will now be ["Hello world!", "It's me!"]
longer_list.append(4)  # will now be [1, 2, 3, 4]
```

Elements can also be removed using the `list.pop` method (without a position argument, the last element in the list will be removed):

```python
empty_list.pop() # will now be []
short_list.pop() # will now be ["Hello world!"]
longer_list.pop(0) # will now be [2, 3, 4], as the element at index 0 was removed
```

Additional methods for adding and removing items from lists are detailed [here](https://docs.python.org/3/tutorial/datastructures.html#more-on-lists). Note that adding and removing items in this way will change the length of the list. Additionally, when items are added or removed at arbitrary positions, this will affect the indices of other items in the list.

If a list containing a certain number of blank or dummy elements is needed, we can create one as follows:

```python
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](https://sentry.io/answers/use-the-null-object-in-python/). Because this list contains values at indices 0 to 9, we can access and reassign them. Therefore, we could use this method with the original code without producing an `IndexError`:

```python
numbers = [None] * 10
for i in range(0, 10):
   numbers[i] = i
```

Note that this list can still be added to and removed from. To create a fixed-width sequence with unchangeable elements, we should use a [tuple](https://docs.python.org/3/tutorial/datastructures.html#tuples-and-sequences) instead of a list.

```python
my_tuple = tuple(range(0,10))
# will produce (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)

print(my_tuple[0]) # will print 0
my_tuple[0] = 1 # will fail with "TypeError: 'tuple' object does not support item assignment"
my_tuple.append(10) # will fail with "AttributeError: 'tuple' object has no attribute 'append'"
my_tuple.pop() # will fail with "AttributeError: 'tuple' object has no attribute 'pop'"
```

If a tuple is too restrictive and we just want to be able to change individual elements while keeping the sequence to maximum length, we can use a [`deque`](https://docs.python.org/3/library/collections.html#collections.deque) from Python's built-in [`collections`](https://docs.python.org/3/library/collections.html) module. This data structure supports an optional maximum length, specified on creation.

```python
from collections import deque

max10 = deque([None] * 10, maxlen=10)

print(max10) # will print deque([None, None, None, None, None, None, None, None, None, None], maxlen=10)

for i in range(0, 10):
   max10[i] = i

print(max10) # will print deque([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], maxlen=10)
```

A *deque* is a double-end queue and has methods for appending to and popping from either side (`append`, `appendleft`, `pop`, `popleft`). Note that if an element is appended to a deque already at its maximum length, the element on the other side of the deque will be popped. For example:

```python
max10.append(10)
print(max10) # will print deque([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], maxlen=10)

max10.appendleft(-1)
print(max10) # will print deque([-1, 1, 2, 3, 4, 5, 6, 7, 8, 9], maxlen=10)
```

Deques can also be smaller than their maximum length.

```python
max10.pop()
max10.popleft()

print(max10) # will print deque([1, 2, 3, 4, 5, 6, 7, 8], maxlen=10)
```

---

*Source: [sentry.io/answers/create-a-list-of-a-given-size-in-python/](https://sentry.io/answers/create-a-list-of-a-given-size-in-python/)*
