Python program to return rows that have element at a specified index
Last Updated :
18 May, 2023
Given two Matrices, the task is to write a Python program that can extract all the rows from both matrices which have similar elements at their Kth index, mapped at similar row positions.
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
# Empty list
res = []
for idx in range(len(test_list1)):
# Comparinging 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))
OutputThe 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]]
Time Complexity: O(n*m)
Auxiliary Space: O(k)
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))
OutputThe 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]]
Time Complexity: O(n*n) where n is the number of elements in the list “test_list”. The list comprehension and zip() are used to perform the task and it takes O(n*n) time.
Auxiliary Space: O(n), new list of size O(n) is created where n is the number of elements in the list.
Method 3: Using the NumPy module
Steps:
- Import the numpy module.
- Convert the input lists to numpy arrays.
- Get the Kth column of each array using array slicing.
- Compare the Kth columns of the arrays using the "==" operator to get a boolean array.
- Use the boolean array to index the original arrays to get the matching rows.
- Combine the matching rows from both arrays using NumPy's "column_stack" function.
- Convert the combined array back to a list of lists.
Python3
import numpy as np
# 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]]
# convert lists to numpy arrays
arr1 = np.array(test_list1)
arr2 = np.array(test_list2)
# get Kth column of each array
k_col1 = arr1[:, 1]
k_col2 = arr2[:, 1]
# compare Kth columns of arrays to get boolean array
bool_arr = k_col1 == k_col2
# use boolean array to index original arrays to get matching rows
matching_arr1 = arr1[bool_arr]
matching_arr2 = arr2[bool_arr]
# combine matching rows from both arrays using vstack function
combined_arr = np.vstack((matching_arr1, matching_arr2))
# convert combined array back to list of lists
res = combined_arr.tolist()
# printing result
print("K index matching rows : " + str(res))
Output:
K index matching rows : [[9, 2, 0], [6, 4, 4], [6, 4, 4], [8, 2, 3], [5, 4, 6], [5, 4, 6]]
Time complexity: O(N), where N is the number of elements in the lists.
Auxiliary space: O(N) as well, since the arrays and the combined array both require N elements of memory.
Similar Reads
Python Program that prints rows from the matrix that have same element at a given index
Given a Matrix, the following article shows how rows which has similar digit at the specified index will be extracted and returned as output. Input : test_list = [[3345, 6355, 83, 938], [323, 923, 845], [192, 993, 49], [98, 34, 23]], K = 1 Output : [[3345, 6355, 83, 938], [192, 993, 49]] Explanation
5 min read
Python Program that prints the rows of a given length from a matrix
Given a Matrix, the following articles shows how to extract all the rows with a specified length. Input : test_list = [[3, 4, 5, 6], [1, 4, 6], [2], [2, 3, 4, 5, 6], [7, 3, 1]], K = 3 Output : [[1, 4, 6], [7, 3, 1]] Explanation : Extracted lists have length of 3.Input : test_list = [[3, 4, 5, 6], [1
4 min read
Python program to remove row with custom list element
Given a matrix, the task here is to write a Python program to remove rows that have any element from the custom list and then display the result. Examples: Input : test_list = [[5, 3, 1], [7, 8, 9], [1, 10, 22], [12, 18, 21]], check_list = [3, 10, 19, 29, 20, 15] Output : [[7, 8, 9], [12, 18, 21]] E
6 min read
Python - Filter rows with required elements
Given a Matrix, filter rows with required elements from other list. Input : test_list = [[2, 4, 6], [7, 4, 3, 2], [2, 4, 8], [1, 1, 9]], check_list = [4, 6, 2, 8] Output : [[2, 4, 6], [2, 4, 8]] Explanation : All elements are from the check list. Input : test_list = [[2, 4, 6], [7, 4, 3, 2], [2, 4,
6 min read
Python Program to find the Next Nearest element in a Matrix
Given a matrix, a set of coordinates and an element, the task is to write a python program that can get the coordinates of the elements next occurrence. Input : test_list = [[4, 3, 1, 2, 3], [7, 5, 3, 6, 3], [8, 5, 3, 5, 3], [1, 2, 3, 4, 6]], i, j = 1, 3, K = 3 Output : (1, 4) Explanation : After (1
4 min read
Python - Filter Rows with Range Elements
Given a Matrix, filter all the rows which contain all elements in the given number range. Input : test_list = [[3, 2, 4, 5, 10], [3, 2, 5, 19], [2, 5, 10], [2, 3, 4, 5, 6, 7]], i, j = 2, 5 Output : [[3, 2, 4, 5, 10], [2, 3, 4, 5, 6, 7]] Explanation : 2, 3, 4, 5 all are present in above rows. Input :
5 min read
Python program to print Rows where all its Elements' frequency is greater than K
Given Matrix, extract all rows whose all elements have a frequency greater than K. Input : test_list = [[1, 1, 2, 3, 2, 3], [4, 4, 5, 6, 6], [1, 1, 1, 1], [4, 5, 6, 8]], K = 2 Output : [[1, 1, 1, 1]] Explanation : Here, frequency of 1 is 4 > 2, hence row retained. Input : test_list = [[1, 1, 2, 3
8 min read
Python program to check if any key has all the given list elements
Given a dictionary with list values and a list, the task is to write a Python program to check if any key has all the list elements. Examples: Input : test_dict = {'Gfg' : [5, 3, 1, 6, 4], 'is' : [8, 2, 1, 6, 4], 'best' : [1, 2, 7, 3, 9], 'for' : [5, 2, 7, 8, 4, 1], 'all' : [8, 5, 3, 1, 2]}, find_li
7 min read
Python Program to check if elements to the left and right of the pivot are smaller or greater respectively
Given a list and an index, the task is to write a Python program to first select the element at that index as the pivot element and then test if elements are greater to its right and smaller to its left or not. Examples: Input : test_list = [4, 3, 5, 6, 9, 16, 11, 10, 12], K = 4 Output : True Explan
3 min read
Python Program that filters out non-empty rows of a matrix
Given Matrix, the following article shows how to filter all the Non-Empty rows of a matrix. In simpler terms, the codes provided below return a matrix after removing empty rows from it. Input : test_list = [[4, 5, 6, 7], [], [], [9, 8, 1], []] Output : [[4, 5, 6, 7], [9, 8, 1]] Explanation : All emp
4 min read