# Initialize a list with given size and values in Python

Posted: 2020-10-19 / Tags: Python, List

This article describes how to initialize a list with an any size (number of elements) and values in Python.

• Create an empty list
• Initialize a list with an any size and values
• Notes on initializing a 2D list (list of lists)
• For tuples and arrays

See the following article about initialization of NumPy array `ndarray`.

## Create an empty list

An empty list is created as follows. You can get the number of elements of a list with the built-in function `len()`.

``````l_empty = []
print(l_empty)
# []

print(len(l_empty))
# 0
``````

You can add an element by `append()` or remove it by `remove()`.

``````l_empty.append(100)
l_empty.append(200)
print(l_empty)
# [100, 200]

l_empty.remove(100)
print(l_empty)
# 
``````

See the following articles for details on adding and removing elements from lists,

## Initialize a list with an any size and values

As mentioned above, in Python, you can easily add and remove elements from a list, so there are few situations where you need to initialize a list in advance.

If you want to initialize a list of any number of elements where all elements are filled with any values, you can use the `*` operator as follows.

``````l =  * 10
print(l)
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

print(len(l))
# 10
``````

A list is generated that repeats the elements of the original list.

``````print([0, 1, 2] * 3)
# [0, 1, 2, 0, 1, 2, 0, 1, 2]
``````

You can generate a list of sequential numbers with `range()`.

## Notes on initializing a 2D list (list of lists)

Be careful when initializing a list of lists.

The following code is no good.

``````l_2d_ng = [ * 4] * 3
print(l_2d_ng)
# [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
``````

If you update one list, all the lists will be changed.

``````l_2d_ng = 5
print(l_2d_ng)
# [[5, 0, 0, 0], [5, 0, 0, 0], [5, 0, 0, 0]]

l_2d_ng.append(100)
print(l_2d_ng)
# [[5, 0, 0, 0, 100], [5, 0, 0, 0, 100], [5, 0, 0, 0, 100]]
``````

This is because the inner lists are all the same object.

``````print(id(l_2d_ng) == id(l_2d_ng) == id(l_2d_ng))
# True
``````

You can write as follows using list comprehensions.

``````l_2d_ok = [ * 4 for i in range(3)]
print(l_2d_ok)
# [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
``````

Each inner list is treated as a different object.

``````l_2d_ok = 100
print(l_2d_ok)
# [[100, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]

print(id(l_2d_ok) == id(l_2d_ok) == id(l_2d_ok))
# False
``````

Although `range()` is used in the above example, any iterable of a desired size is acceptable.

``````l_2d_ok_2 = [ * 4 for i in  * 3]
print(l_2d_ok_2)
# [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]

l_2d_ok_2 = 100
print(l_2d_ok_2)
# [[100, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]

print(id(l_2d_ok_2) == id(l_2d_ok_2) == id(l_2d_ok_2))
# False
``````

If you want to generate a multidimensional list, you can nest list comprehensions.

``````l_3d = [[ * 2 for i in range(3)] for j in range(4)]
print(l_3d)
# [[[0, 0], [0, 0], [0, 0]], [[0, 0], [0, 0], [0, 0]], [[0, 0], [0, 0], [0, 0]], [[0, 0], [0, 0], [0, 0]]]

l_3d = 100
print(l_3d)
# [[[100, 0], [0, 0], [0, 0]], [[0, 0], [0, 0], [0, 0]], [[0, 0], [0, 0], [0, 0]], [[0, 0], [0, 0], [0, 0]]]
``````

## For tuples and arrays

You can initialize tuples as well as lists.

Note that a tuple of one element requires `,`.

``````t = (0,) * 5
print(t)
# (0, 0, 0, 0, 0)
``````

For `array` type, you can pass the initialized list to the constructor.

``````import array

a = array.array('i',  * 5)
print(a)
# array('i', [0, 0, 0, 0, 0])
``````