Create and Initialize two-dimensional lists in Python
This post will discuss how to construct and initialize two-dimensional lists in Python.
You must never initialize a two-dimensional list using the expression [[val]*c]*r. This might result in reference errors since the resultant list contains the same list instance repeated r times. The following example demonstrates this behavior:
|
1 2 3 4 5 6 7 8 9 10 11 |
if __name__ == '__main__': val = 1 (M, N) = (2, 3) # generate an `M × N` matrix initialized with `val` x = [[val] * N] * M x[0][0] = 2 print(x) # [[2, 1, 1], [2, 1, 1]] |
This post provides an overview of some of the available alternatives to create and initialize two-dimensional lists in Python correctly.
1. Using List Comprehension
A simple yet elegant solution to initialize two-dimensional lists in Python is using list comprehensions:
|
1 2 3 4 5 6 7 8 9 10 11 |
if __name__ == '__main__': val = 1 (M, N) = (4, 5) # generate an `M × N` matrix initialized with `val` x = [[val for i in range(N)] for j in range(M)] for r in range(M): print(x[r]) |
This can be further shortened as follows:
|
1 2 3 4 5 6 7 8 9 10 11 |
if __name__ == '__main__': val = 1 (M, N) = (4, 5) # generate an `M × N` matrix initialized with `val` x = [[val]*N for _ in range(M)] for r in range(M): print(x[r]) |
2. Using NumPy
Alternatively, you can use NumPy to declare and initialize two-dimensional lists in Python. Here’s what the code would look like:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import numpy as np if __name__ == '__main__': val = 1 (M, N) = (4, 5) # generate an `M × N` matrix of type `int` x = np.empty((M, N), dtype=int) # initialize the matrix with `val` x.fill(val) for r in range(M): print(x[r]) |
With NumPy 1.8, you can use the np.full() function, which declares and initialize the matrix in a single step.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import numpy as np if __name__ == '__main__': val = 1 (M, N) = (4, 5) # generate an `M × N` matrix initialized with `val` x = np.full((M, N), val, dtype=int) for r in range(M): print(x[r]) |
If you need a matrix with the values initialized with all zeros or ones, you can use the np.zeros() and np.ones() function, respectively:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import numpy as np if __name__ == '__main__': (M, N) = (4, 5) # generate an `M × N` matrix initialized with zeros x = np.zeros((M, N), dtype=int) for r in range(M): print(x[r]) # generate an `M × N` matrix initialized with ones y = np.ones((M, N), dtype=int) for r in range(M): print(x[r]) |
Another plausible way to construct a two-dimensional list with NumPy is to create a single-dimensional list filled with desired values and then reshape it to two-dimensions using the np.reshape() function.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import numpy as np if __name__ == '__main__': val = 1 (M, N) = (5, 5) # generate an `M × N` matrix initialized with `val` x = np.list([val] * M * N).reshape((M, N)) for r in range(M): print(x[r]) |
That’s all about declaring and initializing two-dimensional lists in Python.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)