Python program to return rows that have element at a specified index
Given two Matrices, the task is to write a Python program that can extract all the rows from both matrix which have similar element at its Kth index, mapped at similar row position.
Examples:
Input : test_list1 = [[1, 8, 3], [9, 2, 0], [6, 4, 4], [6, 4, 4]], test_list2 = [[1, 9, 3], [8, 2, 3], [5, 4, 6], [5, 4, 6]], K = 1
Output : [[9, 2, 0], [8, 2, 3], [6, 4, 4], [5, 4, 6], [6, 4, 4], [5, 4, 6]]
Explanation : All elements with similar elements at 1st index extracted.
Input : test_list1 = [[1, 8, 3], [9, 2, 0], [6, 4, 4], [6, 5, 4]], test_list2 = [[1, 9, 3], [8, 2, 3], [5, 4, 6], [5, 4, 6]], K = 1
Output : [[9, 2, 0], [8, 2, 3], [6, 4, 4], [5, 4, 6]]
Explanation : All elements with similar elements at 1st index extracted.
Method 1 : Using loop and enumerate()
In this, the list is iterated from the start row till the end, and each row’s Kth index is matched, if found, both rows are appended to the result.
Example:
Python3
# initializing lists test_list1 = [[ 1 , 8 , 3 ], [ 9 , 2 , 0 ], [ 6 , 4 , 4 ], [ 6 , 4 , 4 ]] test_list2 = [[ 1 , 9 , 3 ], [ 8 , 2 , 3 ], [ 5 , 4 , 6 ], [ 5 , 4 , 6 ]] # printing original lists print ( "The original list 1 is : " + str (test_list1)) print ( "The original list 2 is : " + str (test_list2)) # initializing K K = 1 res = [] for idx in range ( len (test_list1)): # comparing lists if test_list1[idx][K] = = test_list2[idx][K]: res.append(test_list1[idx]) res.append(test_list2[idx]) # printing result print ( "K index matching rows : " + str (res)) |
Output:
The original list 1 is : [[1, 8, 3], [9, 2, 0], [6, 4, 4], [6, 4, 4]]
The original list 2 is : [[1, 9, 3], [8, 2, 3], [5, 4, 6], [5, 4, 6]]
K index matching rows : [[9, 2, 0], [8, 2, 3], [6, 4, 4], [5, 4, 6], [6, 4, 4], [5, 4, 6]]
Method 2: Using list comprehension and zip()
In this, we perform the task of getting pairing using zip(), and then compare the Kth element, append and iterate using extend() and list comprehension.
Example:
Python3
# initializing lists test_list1 = [[ 1 , 8 , 3 ], [ 9 , 2 , 0 ], [ 6 , 4 , 4 ], [ 6 , 4 , 4 ]] test_list2 = [[ 1 , 9 , 3 ], [ 8 , 2 , 3 ], [ 5 , 4 , 6 ], [ 5 , 4 , 6 ]] # printing original lists print ( "The original list 1 is : " + str (test_list1)) print ( "The original list 2 is : " + str (test_list2)) # initializing K K = 1 # zip() combines elements together res = [] [res.extend([t1, t2]) for t1, t2 in zip (test_list1, test_list2) if t1[K] = = t2[K]] # printing result print ( "K index matching rows : " + str (res)) |
Output:
The original list 1 is : [[1, 8, 3], [9, 2, 0], [6, 4, 4], [6, 4, 4]]
The original list 2 is : [[1, 9, 3], [8, 2, 3], [5, 4, 6], [5, 4, 6]]
K index matching rows : [[9, 2, 0], [8, 2, 3], [6, 4, 4], [5, 4, 6], [6, 4, 4], [5, 4, 6]]