Python 2 DArray
Python 2 DArray
N=5
ar = [0]*N
print(ar)
Output
[0, 0, 0, 0, 0]
N = 5
arr = [0 for i in range(N)]
print(arr)
Output
[0, 0, 0, 0, 0]
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]]
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.
rows, cols = (5, 5)
arr = [[0]*cols]*rows
print(arr, "before")
print(arr, "after")
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]], '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')
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]]
Creating 1D List using Empty List
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.
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]]
Initializing 2D Array
The provided code demonstrates two different approaches to initializing a 2D array
in Python. First, the array arr is initialized using a 2D list comprehension, where each
row is created as [0, 0, 0, 0, 0]. The entire array is created as a list of references to
the same inner list, resulting in aliasing. Any change made to an element in one row
will be reflected in all rows. The code then shows another approach using a nested list
comprehension to create the 2D array arr. This method avoids aliasing by creating a
new list for each row, resulting in a proper 2D array.
arr = [[0]*cols]*rows
arr[0][0] = 1
for row in arr:
print(row)
arr[0][0] = 1
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]
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
by using the ‘is’ operator which checks if the two operands refer to the same object.
rows, cols = (5, 5)
arr = [[0]*cols]*rows
# check if arr[0] and arr[1] refer to the same object prints True because there is only one
print(arr[0] is arr[1])
Output
False
True
size = int(input())
array_input = []
for x in range(size):
array_input.append([int(y) for y in input().split()])
print(array_input)
How to Insert elements in a 2-D array?
Elements in a 2D array can be inserted using the insert() function
specifying the index/position of the element to be inserted.
from array import *
print(input)
input.insert(1, [1,3,5,7,9])
for x in input:
for y in x:
print()
print(input)
input[0] = [10,8]
input[1][1] = 9
for x in input:
for y in x:
print()
Output:
The elements from a 2-D array can be deleted using del() method.
from array import *
print(input)
del(input[1])
for x in input:
for y in x:
print()
Output:
print(len(array_input))
output 2