![]() |
VOOZH | about |
Python provides flexible data structures such as lists, which can be used to represent 1D and 2D arrays. A 2D list in Python is essentially a list of lists, commonly used to store data in a table-like format with rows and columns.
This article focuses on correct and incorrect ways to create 1D and 2D lists in Python.
A 1D list stores elements in a linear sequence. Although Python does not have a native 1D array type, lists serve the same purpose efficiently.
Manually initializing and populating a list without using any advanced features or constructs in Python is known as creating a 1D list using "Naive Methods".
[0, 0, 0, 0, 0]
Explanation: [0] * N creates a list of size N.
[0, 0, 0, 0, 0]
Explanation: range(N) controls list length.
A 2D list represents data in rows and columns. Correct initialization is important to avoid unintended shared references.
Here we are multiplying the number of columns and hence we are getting the 1-D list of size equal to the number of columns and then multiplying it with the number of rows which results in the creation of a 2-D list.
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
Explanation:
Note: Using this method can sometimes cause unexpected behaviors. In this method, each row will be referencing the same column. This means, even if we update only one element of the array, it will update the same column in our array.
([[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]], 'before') ([[1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0]], 'after')
Here we are basically using the concept of list comprehension and applying a loop for a list inside a list and hence creating a 2-D list.
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
Explanation:
Here we are appending zeros as elements for a number of columns times and then appending this 1-D list into the empty row list and hence creating the 2-D list.
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
Explanation:
The code below, compares two ways of initializing a 2D list in Python. Using list multiplication ([[0]*cols]*rows) creates multiple references to the same inner list, causing aliasing where changes affect all rows. Using a nested list comprehension creates a separate list for each row, avoiding aliasing and correctly forming a 2D array.
[1, 0, 0, 0, 0] [1, 0, 0, 0, 0] [1, 0, 0, 0, 0] [1, 0, 0, 0, 0] [1, 0, 0, 0, 0] [1, 0, 0, 0, 0] [0, 0, 0, 0, 0] [0, 0, 0, 0, 0] [0, 0, 0, 0, 0] [0, 0, 0, 0, 0]
Explanation:
If we assign the 0th index to another integer say 1, then a new integer object is created with the value of 1 and then the 0th index now points to this new int object as shown below
Similarly, when we create a 2d array as "arr = [[0]*cols]*rows" we are essentially extending the above analogy.
The above setup can be visualized in the image below.
Now lets change the first element in first row of "arr" as arr[0][0] = 1
This can be clearly seen in the below image.
So when 2d arrays are created like this, changing values at a certain row will affect all the rows since there is essentially only one integer object and only one list object being referenced by the all the rows of the array.
Tracing out errors caused by such usage of shallow lists is difficult. Hence the better way to declare a 2d array is
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
This method creates 5 separate list objects, unlike method 2a. One way to check this is by using the 'is' operator which checks if the two operands refer to the same object.
False True
Explanation: