Create 2d array python without numpy

This is probably duplicate question, but I am still curious about this.

I want to make two-dimensional list in Python without numpy. So I make a list of list. Here is my code:

myList = [None] * 3
print['myList :', myList]
myMatrix = [myList] * 3
#myMatrix = [[None, None, None], [None, None, None], [None, None, None]]
print['myMatrix', myMatrix]
for i in range [0,3]:
    for j in range [0, 3]:
        myMatrix[i][j] = i+j
    print['myMatrix[',i,'] : ', myMatrix[i]]

print[myMatrix]
print[myMatrix[0]]
print[myMatrix[1]]
print[myMatrix[2]]

I know that the statement:

myMatrix = [myList] * 3

makes the code failed to work as I expected, and it is because list is mutable object, which means myMatrix[0], myMatrix[1], myMatrix[2] will refer to the same object. A change to any of them means a change to all of them, which is not what I expected in my code. Here is the unexpected output of my code:

['myList :', [None, None, None]]
['myMatrix', [[None, None, None], [None, None, None], [None, None, None]]]
['myMatrix[', 0, '] : ', [0, 1, 2]]
['myMatrix[', 1, '] : ', [1, 2, 3]]
['myMatrix[', 2, '] : ', [2, 3, 4]]
[[2, 3, 4], [2, 3, 4], [2, 3, 4]]
[2, 3, 4]
[2, 3, 4]
[2, 3, 4]

The only solution I found is, instead of stating myMatrix = [myList] * 3, I should write:

myMatrix = [[None, None, None], [None, None, None], [None, None, None]]

That will make the code works as I expected below [the output of the program]:

['myMatrix', [[None, None, None], [None, None, None], [None, None, None]]]
['myMatrix[', 0, '] : ', [0, 1, 2]]
['myMatrix[', 1, '] : ', [1, 2, 3]]
['myMatrix[', 2, '] : ', [2, 3, 4]]
[[0, 1, 2], [1, 2, 3], [2, 3, 4]]
[0, 1, 2]
[1, 2, 3]
[2, 3, 4]

But it's not an efficient way to define a NxN matrix, especially if N is a big number.

Does Python have an efficient way to define a NxN matrix using list of list?

I'm more familiar with C/C++, so this problem is really bugging me. Some answers will recommend me to use numpy, but at this moment I would like to learn basic Python from scratch [without importing any library]. When I use C/C++, I can work with this two-dimensional array easily, without importing any library. Asking me to use numpy when I'm just new in Python, is just like asking me to use STL when I'm just new to C.

Of course I will learn numpy later, but I want to tackle this without numpy first.

Python provides many ways to create 2-dimensional lists/arrays. However one must know the differences between these ways because they can create complications in code that can be very difficult to trace out. Let’s start by looking at common ways of creating 1d array of size N initialized with 0s.

Using 2D arrays/lists the right way

Method 1: Creating a 1-D list

Example 1: Creating 1d list Using Naive methods

Python3

Example 2: creating 1d list using  List Comprehension

Python3

N = 5

arr = [0 for i in range[N]]

print[arr]

Explanation:

Here we are multiplying the number of rows to the empty list and hence a entire list is created with every element is zero.

Method 2 Creating a 2-D list

Example 1: Naive Method

Python3

rows, cols = [5, 5]

arr = [[0]*cols]*rows

print[arr]

Output

[[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 multiplying the number of columns  and hence we are getting the 1-D list of size equal to number of columns and then multiplying it with the number of rows which results in the creation of a 2-D list.

Example 2: Using List Comprehension

Python3

rows, cols = [5, 5]

arr = [[0 for i in range[cols]] for j in range[rows]]

print[arr]

Output

[[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 basically using the concept of list comprehension and applying loop for list inside a list and hence creating a 2-D list.

Example 3: Using empty list

Python3

arr=[]

rows, cols=5,5

for i in range[rows]:

    col = []

    for j in range[cols]:

        col.append[0]

    arr.append[col]

print[arr]

Output

[[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 number of columns times and then appending this 1-D list into the empty row list and hence creating the 2-D list.

Python3

rows, cols = [5, 5]

arr = [[0]*cols]*rows

arr[0][0] = 1

for row in arr:

    print[row]

arr = [[0 for i in range[cols]] for j in range[rows]]

arr[0][0] = 1

for row in arr:

    print[row]

Output

[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:

We expect only the first element of first row to change to 1 but the first element of every row gets changed to 1 in method 2a. This peculiar functioning is because Python uses shallow lists which we will try to understand.
In method 1a, Python doesn’t create 5 integer objects but creates only one integer object and all the indices of the array arr point to the same int object as shown.

If we assign the 0th index to a 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 the extending the above analogy. 

  1. Only one integer object is created. 
  2. A single 1d list is created and all its indices point to the same int object in point 1. 
  3. Now, arr[0], arr[1], arr[2] …. arr[n-1] all point to the same list object above in point 2.

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

  • arr[0] points to the single list object we created we above.[Remember arr[1], arr[2] …arr[n-1] all point to the same list object too].
  • The assignment of arr[0][0] will create a new int object with the value 1 and arr[0][0] will now point to this new int object.[and so will arr[1][0], arr[2][0] … arr[n-1][0]]

This can be clearly seen in the below image. 

So when 2d arrays are created like this, changing values at a certain row will effect 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.

As you would expect, tracing out errors caused by such usage of shallow lists is difficult. Hence the better way to declare a 2d array is 

Python3

rows, cols = [5, 5]

print[[[0 for i in range[cols]] for j in range[rows]]]

Output

[[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 using the ‘is’ operator which checks if the two operands refer to the same object. 

Python3

rows, cols = [5, 5]

arr = [[0 for i in range[cols]] for j in range[rows]]

print[arr[0] is arr[1]]

arr = [[0]*cols]*rows

print[arr[0] is arr[1]]


Can you make 2D arrays in Python?

Python provides many ways to create 2-dimensional lists/arrays.

How do you create a 2D array in Python?

We can insert elements into a 2 D array using the insert[] function that specifies the element' index number and location to be inserted. # Write a program to insert the element into the 2D [two dimensional] array of Python. from array import * # import all package related to the array.

How do you create an empty two dimensional array in Python?

Add multiple columns to an empty 2D Numpy array in single line.
# Create an empty 2D numpy array with 4 rows and 0 column..
empty_array = np. empty[[4, 0], int].
column_list_2 = np. ... .
# Append list as a column to the 2D Numpy array..
empty_array = np. ... .
print['2D Numpy array:'].
print[empty_array].

How do you make a two

myList = [0,1,2,3,4,5,6,7,8,9]; for index in len[myList]: myList[index] = 0 # Set element at "index" to 0. For a two-dimensional list, in order to reference every element, we must use two nested loops. This gives us a counter variable for every column and every row in the matrix.

Chủ Đề