Python - K Matrix Initialization
Last Updated :
17 Apr, 2023
Sometimes in the world of competitive programming, we need to initialise the matrix, but we don’t wish to do it in a longer way using a loop. We need a shorthand for this. This type of problem is quite common in dynamic programming domain. Let’s discuss certain ways in which this can be done.
Method #1: Using List comprehension List comprehension can be treated as a shorthand for performing this particular operation. In list comprehension, we can initialise the inner list with K and then extend this logic to each row again using the list comprehension.
Python3
# Python3 code to demonstrate
# K Matrix Initialization
# using list comprehension
# Declaring rows
N = 5
# Declaring columns
M = 4
# initializing K
K = 7
# using list comprehension
# to initializing matrix
res = [ [ K for i in range(N) ] for j in range(M) ]
# printing result
print("The matrix after initializing with K : " + str(res))
Output : The matrix after initializing with K : [[7, 7, 7, 7, 7], [7, 7, 7, 7, 7], [7, 7, 7, 7, 7], [7, 7, 7, 7, 7]]
Time Complexity: O(n * m) where n is the number of rows and m is the number of columns
Auxiliary Space: O(n * m) where n is the number of rows and m is the number of columns
Method #2: Using list comprehension + “*” operator This problem can also be simplified using the * operator which can slightly reduce the tedious way task is done and can simply use multiply operator to extent the initialization to all N rows.
Python3
# Python3 code to demonstrate
# K Matrix Initialization
# using list comprehension
# and * operator
# Declaring rows
N = 5
# Declaring columns
M = 4
# initializing K
K = 7
# using list comprehension
# to initializing matrix
res = [ [K for i in range(M)] * N]
# printing result
print("The matrix after initializing with K : " + str(res))
Output : The matrix after initializing with K : [[7, 7, 7, 7, 7], [7, 7, 7, 7, 7], [7, 7, 7, 7, 7], [7, 7, 7, 7, 7]]
Method #3: Using Numpy
Note: Install numpy module using command "pip install numpy"
One can also use the Numpy library to initialise the matrix. Numpy library provides a convenient way to initialise the matrix of given size and fill it with any given value.
Python3
import numpy as np
# Declaring rows
N = 5
# Declaring columns
M = 4
# initializing K
K = 7
# using numpy library
# to initializing matrix
res = np.full((N, M), K)
# printing result
print("The matrix after initializing with K :\n", res)
#This code is contributed by Edula Vinay Kumar Reddy
Output:
The matrix after initializing with K :
[[7 7 7 7]
[7 7 7 7]
[7 7 7 7]
[7 7 7 7]
[7 7 7 7]]
Time Complexity: O(n * m) where n is the number of rows and m is the number of columns
Auxiliary Space: O(n * m) where n is the number of rows and m is the number of columns
Method 4: using a nested for loop.
Step-by-step approach:
- Declare the number of rows and columns N and M.
- Initialize the value K.
- Create an empty list res to store the matrix.
- Use a nested for loop to iterate over the rows and columns and append K to the matrix.
- Print the matrix.
Below is the implementation of the above approach:
Python3
# Python3 code to demonstrate
# K Matrix Initialization
# using nested for loop
# Declaring rows
N = 5
# Declaring columns
M = 4
# initializing K
K = 7
# creating an empty matrix
res = []
# using nested for loop
for i in range(M):
row = []
for j in range(N):
row.append(K)
res.append(row)
# printing result
print("The matrix after initializing with K : " + str(res))
OutputThe matrix after initializing with K : [[7, 7, 7, 7, 7], [7, 7, 7, 7, 7], [7, 7, 7, 7, 7], [7, 7, 7, 7, 7]]
Time Complexity: O(NM)
Auxiliary Space: O(NM) to store the matrix.
Method #5: Using list multiplication
Step-by-step Approach:
- Declare the number of rows and columns in the matrix as N and M respectively.
- Initialize the value of K that will be used to initialize the matrix.
- Create an empty list res that will be used to store the matrix.
- Create a list of K values with length N using the list multiplication operator *. This will be used to initialize each row of the matrix.
- Use a list comprehension to create the matrix by repeating the row M times.
- Print the resulting matrix.
Python3
# Python3 code to demonstrate K Matrix
# Initialization using list multiplication
# Declaring rows
N = 5
# Declaring columns
M = 4
# initializing K
K = 7
# creating a list of K values with length N
row = [K] * N
# using list multiplication to create the matrix
res = [row for _ in range(M)]
# printing result
print("The matrix after initializing with K : " + str(res))
OutputThe matrix after initializing with K : [[7, 7, 7, 7, 7], [7, 7, 7, 7, 7], [7, 7, 7, 7, 7], [7, 7, 7, 7, 7]]
Time Complexity: O(N*M), as each element in the matrix is initialized once.
Auxiliary Space: O(N*M), as the entire matrix is stored in memory.
Similar Reads
Initialize Matrix in Python Initializing a matrix in Python refers to creating a 2D structure (list of lists) to store data in rows and columns. For example, a 3x2 matrix can be represented as three rows, each containing two elements. Letâs explore different methods to do this efficientely.Using list comprehensionList comprehe
2 min read
Python - Incremental K sized Row Matrix Initialization Sometimes, while working with Python, we can have a problem in which we need to perform initialization of matrix with Incremental numbers. This kind of application can come in Data Science domain. Let's discuss certain ways in which this task can be performed. Method #1: Using loop + list slicing Th
6 min read
Python - Incremental Range Initialization in Matrix Sometimes, while working with Python, we can have a problem in which we need to perform the initialization of Matrix. Simpler initialization is easier. But sometimes, we need to perform range incremental initialization. Let's discuss certain ways in which this task can be performed. Method #1: Using
4 min read
Python - Initialize dictionary keys with Matrix Sometimes, while working with Python Data, we can have a problem in which we need to construct an empty mesh of dictionaries for further population of data. This problem can have applications in many domains which include data manipulation. Let's discuss certain ways in which this task can be perfor
4 min read
Python - Convert Matrix to Dictionary The task of converting a matrix to a dictionary in Python involves transforming a 2D list or matrix into a dictionary, where each key represents a row number and the corresponding value is the row itself. For example, given a matrix li = [[5, 6, 7], [8, 3, 2], [8, 2, 1]], the goal is to convert it i
4 min read