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
N = 5
M = 4
K = 7
res = [ [ K for i in range (N) ] for j in range (M) ]
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
N = 5
M = 4
K = 7
res = [ [K for i in range (M)] * N]
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
N = 5
M = 4
K = 7
res = np.full((N, M), K)
print ( "The matrix after initializing with K :\n" , 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 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
N = 5
M = 4
K = 7
res = []
for i in range (M):
row = []
for j in range (N):
row.append(K)
res.append(row)
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(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
N = 5
M = 4
K = 7
row = [K] * N
res = [row for _ in range (M)]
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), 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
There are many ways to declare a 2 dimensional array with given number of rows and columns. Let us look at some of them and also at the small but tricky catches that accompany it. We can do it using list comprehension, concatenation feature of * operator and few other ways. Method 0: 2 list comprehe
4 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 | Initializing multiple lists
In real applications, we often have to work with multiple lists, and initialize them with empty lists hampers the readability of code. Hence a one-liner is required to perform this task in short so as to give a clear idea of the type and number of lists declared to be used. Method #1: Using loops We
4 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
Python | Matrix creation of n*n
Matrices are fundamental structures in programming and are widely used in various domains including mathematics, machine learning, image processing, and simulations. Creating an nÃn matrix efficiently is an important step for these applications. This article will explore multiple ways to create such
3 min read
Python Initialize List of Lists
A list of lists in Python is often used to represent multidimensional data such as rows and columns of a matrix. Initializing a list of lists can be done in several ways each suited to specific requirements such as fixed-size lists or dynamic lists. Let's explore the most efficient and commonly used
3 min read
Python - Convert Coordinate Dictionary to Matrix
Sometimes, while working with Python Matrix, we can have problem in which we have dictionary records with key as matrix position and its value, and we wish to convert that to actual Matrix. This can have applications in many domains including competitive programming and day-day programming. Lets dis
4 min read
Python - Convert Matrix to Coordinate Dictionary
Sometimes, while working with Python dictionaries, we can have problem in which we need to perform the conversion of matrix elements to their coordinate list. This kind of problem can come in many domains including day-day programming and competitive programming. Lets discuss certain ways in which t
3 min read