
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python Program to convert a list into matrix with size of each row increasing by a number
When it is required to convert a list into matrix with the size of every row increasing by a number, the ‘//’ operator and a simple iteration is used.
Example
Below is a demonstration of the same
my_list = [42, 45, 67, 89, 99, 10, 23, 12, 31, 43, 60, 1, 0] print("The list is :") print(my_list) my_key = 3 print("The value of key is ") print(my_key) my_result = [] for index in range(0, len(my_list) // my_key): my_result.append(my_list[0: (index + 1) * my_key]) print("The resultant matrix is :") print(my_result)
Output
The list is : [42, 45, 67, 89, 99, 10, 23, 12, 31, 43, 60, 1, 0] The value of key is 3 The resultant matrix is : [[42, 45, 67], [42, 45, 67, 89, 99, 10], [42, 45, 67, 89, 99, 10, 23, 12, 31], [42, 45, 67, 89, 99, 10, 23, 12, 31, 43, 60, 1]]
Explanation
A list is defined and is displayed on the console.
A value for key is defined and is displayed on the console.
An empty list is created.
A simple iteration is used along with ‘//’ operator, and the element from a specific index multiplied with key.
This is appended to the empty list.
This list is displayed as output on the console.
- Related Articles
- Program to find number of operations needed to convert list into non-increasing list in Python
- Python program to convert a list of tuples into Dictionary
- Python program to find the redundancy rates for each row of a matrix
- Python program to convert a list into a list of lists using a step value
- Program to find smallest intersecting element of each row in a matrix in Python
- Python Program to Convert List into Array
- Python program to Sort a List of Tuples in Increasing Order by the Last Element in Each Tuple
- Program to find number of increasing subsequences of size k in Python
- How to convert a matrix column into list in R?
- How to convert matrix rows into a list in R?
- Python program to Convert Matrix to Dictionary Value List
- Convert list with varying number of elements into a data frame.
- Python program to convert a list of strings with a delimiter to a list of tuple
- Python Program – Convert String to matrix having K characters per row
- Sort a List of Tuples in Increasing Order by the Last Element in Each Tuple using Python program

Advertisements