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. Lets discuss certain ways in which this task can be performed.
Method #1 : Using loop
This is brute way in which this task can be performed. In this, we iterate through the list and increase the value of element with the range specified.
# Python3 code to demonstrate # Incremental Range Initialization in Matrix # using loop # Initializing row r = 4 # Initializing col c = 3 # Initializing range rang = 5 # Incremental Range Initialization in Matrix # using loop res = [] temp = [] temp2 = 0for idx in range(r): for idx in range(c): temp.append(temp2) temp2 = temp2 + rang res.append(temp) temp = [] # printing result print ("Matrix after Initialization : " + str(res)) |
Matrix after Initialization : [[0, 5, 10], [15, 20, 25], [30, 35, 40], [45, 50, 55]]
Method #2 : Using list comprehension
This task can also be performed using list comprehension. The method is similar to above. The difference is this is a shorthand to perform this task.
# Python3 code to demonstrate # Incremental Range Initialization in Matrix # using list comprehension # Initializing row r = 4 # Initializing col c = 3 # Initializing range rang = 5 # Incremental Range Initialization in Matrix # using list comprehension res = [[rang * c * y + rang * x for x in range(c)] for y in range(r)] # printing result print ("Matrix after Initialization : " + str(res)) |
Matrix after Initialization : [[0, 5, 10], [15, 20, 25], [30, 35, 40], [45, 50, 55]]
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Python - Incremental K sized Row Matrix Initialization
- Python - Incremental value initialization in Dictionary
- Python - K Matrix Initialization
- PYGLET – Setting Selection Range in Incremental Text Layout
- Counters in Python | Set 1 (Initialization and Updation)
- Python | Boolean list initialization
- Python | List Initialization with alternate 0s and 1s
- Python | Interval Initialization in list
- Python | Dictionary initialization with common dictionary
- Python - Custom dictionary initialization in list
- Python | Incremental slice partition in list
- Python | N element incremental tuples
- Python - Incremental List Extension
- Python | Incremental Records Product
- Python - Incremental Sublist Sum
- Python - Incremental and Cyclic Repetition of List Elements
- Python | Reverse Incremental String Slicing
- Incremental Programming in Python
- Python - Incremental Size Chunks from Strings
- Python - Incremental Slice concatenation in String list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

