Open In App

Python – Assigning Subsequent Rows to Matrix first row elements

Last Updated : 14 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a (N + 1) * N Matrix, assign each column of 1st row of matrix, the subsequent row of Matrix.

Input : test_list = [[5, 8, 10], [2, 0, 9], [5, 4, 2], [2, 3, 9]] Output : {5: [2, 0, 9], 8: [5, 4, 2], 10: [2, 3, 9]} 
Explanation : 5 paired with 2nd row, 8 with 3rd and 10 with 4th 
Input : test_list = [[5, 8], [2, 0], [5, 4]] Output : {5: [2, 0], 8: [5, 4]} 
Explanation : 5 paired with 2nd row, 8 with 3rd.

Method #1: Using dictionary comprehension

This is one of the ways in which this task can be performed. In this, we iterate for rows and corresponding columns using loop and assign value list accordingly in a one-liner way using dictionary comprehension.

Python3




# Python3 code to demonstrate working of
# Assigning Subsequent Rows to Matrix first row elements
# Using dictionary comprehension
 
# initializing list
test_list = [[5, 8, 9], [2, 0, 9], [5, 4, 2], [2, 3, 9]]
 
# printing original list
print("The original list : " + str(test_list))
 
# pairing each 1st col with next rows in Matrix
res = {test_list[0][ele] :  test_list[ele + 1] for ele in range(len(test_list) - 1)}
 
# printing result
print("The Assigned Matrix : " + str(res))


Output

The original list : [[5, 8, 9], [2, 0, 9], [5, 4, 2], [2, 3, 9]]
The Assigned Matrix : {5: [2, 0, 9], 8: [5, 4, 2], 9: [2, 3, 9]}

Time complexity: O(m*n), because it performs the same number of iterations as the original code.
Auxiliary space: O(m*n) as well, because it creates a dictionary with m * n keys and a list of m * n elements

Method #2 : Using zip() + list slicing + dict()

This is yet another way in which this task can be performed. In this, we slice elements to be first row and subsequent rows using list slicing and zip() performs the task of required grouping. Returned 

Python3




# Python3 code to demonstrate working of
# Assigning Subsequent Rows to Matrix first row elements
# Using zip() + list slicing + dict()
 
# initializing list
test_list = [[5, 8, 9], [2, 0, 9], [5, 4, 2], [2, 3, 9]]
 
# printing original list
print("The original list : " + str(test_list))
 
# dict() to convert back to dict type
# slicing and pairing using zip() and list slicing
res = dict(zip(test_list[0], test_list[1:]))
 
# printing result
print("The Assigned Matrix : " + str(res))


Output

The original list : [[5, 8, 9], [2, 0, 9], [5, 4, 2], [2, 3, 9]]
The Assigned Matrix : {5: [2, 0, 9], 8: [5, 4, 2], 9: [2, 3, 9]}

Method #3 : Using for loop and slicing

Python3




# Python3 code to demonstrate working of
# Assigning Subsequent Rows to Matrix first row elements
 
# initializing list
test_list = [[5, 8, 9], [2, 0, 9], [5, 4, 2], [2, 3, 9]]
 
# printing original list
print("The original list : " + str(test_list))
 
# pairing each 1st col with next rows in Matrix
a=test_list[0]
b=test_list[1:len(test_list)]
d=dict()
for i in range(0,len(a)):
    d[a[i]]=b[i]
     
 
# printing result
print("The Assigned Matrix : " + str(d))


Output

The original list : [[5, 8, 9], [2, 0, 9], [5, 4, 2], [2, 3, 9]]
The Assigned Matrix : {5: [2, 0, 9], 8: [5, 4, 2], 9: [2, 3, 9]}


Previous Article
Next Article

Similar Reads

Python - Assigning Key values to list elements from Value list Dictionary
Given a List of elements, map them with keys of matching values from a value list. Input : test_list = [4, 6, 3, 5, 3], test_dict = {"Gfg" : [5, 3, 6], "is" : [8, 4]} Output : ['is', 'Gfg', 'Gfg', 'Gfg', 'Gfg'] Explanation : 4 is present in "is" key, hence mapped in new list. Input : test_list = [6, 3, 5, 3], test_dict = {"Gfg" : [5, 3, 6], "is" :
7 min read
Python Program to Interchange elements of first and last rows in matrix
Given a 4 x 4 matrix, we have to interchange the elements of first and last row and show the resulting matrix. Examples : Input : 3 4 5 0 2 6 1 2 2 7 1 2 2 1 1 2 Output : 2 1 1 2 2 6 1 2 2 7 1 2 3 4 5 0 Input : 9 7 5 1 2 3 4 1 5 6 6 5 1 2 3 1 Output : 1 2 3 1 2 3 4 1 5 6 6 5 9 7 5 1 Method 1:The approach is very simple, we can simply swap the eleme
4 min read
Assigning multiple variables in one line in Python
A variable is a segment of memory with a unique name used to hold data that will later be processed. Although each programming language has a different mechanism for declaring variables, the name and the data that will be assigned to each variable are always the same. They are capable of storing values of data types. The assignment operator(=) assi
2 min read
Create integer variable by assigning binary value in Python
Given a binary value and our task is to create integer variables and assign values in binary format. To assign value in binary format to a variable, we use the 0b suffix. It tells the compiler that the value (suffixed with 0b) is a binary value and assigns it to the variable. Input: Var = 0b1010 Output: 10 Input: Var = 0b11001 Output: 25 Note: To p
2 min read
PyQt5 QCalendarWidget - Assigning Base Size value
In this article, we will see how we can set the base size value to the QCalendarWidget. By default base size has a value zero for both width and height, base size is used to calculate a proper calendar size if the calendar defines sizeIncrement i.e its size change when window size changes. Base size is the initial size of the calendar. In order to
1 min read
PyQt5 QCommandLinkButton - Assigning Object Name
In this article we will see how we can set the object name of the QCommandLinkButton. Object name is given for searching the specific button inside the window programmatically, also object name can be used for setting stylesheet using object name. By default developer has not set any object name to the command link although we can set it any time.
1 min read
PyQt5 QCommandLinkButton - Assigning Cursor
In this article we will see how we can set i.e assign cursor to the QCommandLinkButton. Assigning cursor means the mouse cursor will assume the new shape when it's over the command link button. Cursor shape is basically cursor icons these are used to classify the actions. In order to do this we use setCursor method with the command link button obje
1 min read
PyQt5 QDateEdit - Assigning Description
In this article we will see how we can assign a description to the QDateEdit. Description property holds the date edit's description as seen by assistive technologies. The accessible description of a date edit should convey what date edit does. While the accessible name should be a short and concise string (e.g. Office Date), the description should
2 min read
PyQt5 QDateEdit - Assigning Name Property
In this article we will see how we can assign name to the QDateEdit. Name property holds the date edit's name as seen by assistive technologies. This is the primary name by which assistive technology such as screen readers announce the given date edit. In order to do this we use setAccessibleName method with the QDateEdit object Syntax : date.setAc
1 min read
MoviePy – Assigning Audio Clip to Video File
In this article we will see how we can add external audio to the video file clip in MoviePy. MoviePy is a Python module for video editing, which can be used for basic operations on videos and GIF’s. Video is formed by the frames, combination of frames creates a video each frame is an individual image. An audio file format is a file format for stori
2 min read
three90RightbarBannerImg