Python – Extract ith column values from jth column values
Sometimes, while working with Python Matrix, we can have a problem in which we need to extract ith column values from comparing values from jth column. This kind of problem can occur in domains such as school programming or web development. Let’s discuss certain ways in which this task can be performed.
Input : test_list = [[4, 5, 6], [2, 5, 7], [9, 8, 2], [10, 2, 6]], search_list = [4, 9], search_idx = 0, ext_idx = 2
Output : [6, 2]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. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
Input : test_list = [[4, 5, 6], [2, 5, 7], [9, 8, 2], [10, 2, 6]], search_list = [2, 6], search_idx = 2, ext_idx = 0
Output : [4, 9, 10]
Method #1 : Using loop
This is brute way to solve this problem. In this, we loop through each row and compare the jth column with list elements, if present, extract the ith element.
# Python3 code to demonstrate working of # Extract ith column values from jth column values# Using loop # initializing listtest_list = [[4, 5, 6], [2, 5, 7], [9, 8, 2], [10, 2, 6]] # printing original listprint("The original list is : " + str(test_list)) # initializing list search_list = [5, 2] # initializing search index search_idx = 1 # initializing extract indexext_idx = 2 # Extract ith column values from jth column values# Using loopres = []for sub in test_list: if sub[search_idx] in search_list: res.append(sub[ext_idx]) # printing result print("The extracted elements : " + str(res)) |
The original list is : [[4, 5, 6], [2, 5, 7], [9, 8, 2], [10, 2, 6]] The extracted elements : [6, 7, 6]
Method #2 : Using set() + list comprehension
The combination of above functions can be used to solve this problem. In this, we perform the task of extracting elements in a similar way as above method, just using shorthand. Also, for initial data search reduction, the search list is converted to set(), to remove duplicated.
# Python3 code to demonstrate working of # Extract ith column values from jth column values# Using set() + list comprehension # initializing listtest_list = [[4, 5, 6], [2, 5, 7], [9, 8, 2], [10, 2, 6]] # printing original listprint("The original list is : " + str(test_list)) # initializing list search_list = [5, 2] # initializing search index search_idx = 1 # initializing extract indexext_idx = 2 # Extract ith column values from jth column values# Using set() + list comprehensiontemp = set(search_list)res = [sub[ext_idx] for sub in test_list if sub[search_idx] in search_list] # printing result print("The extracted elements : " + str(res)) |
The original list is : [[4, 5, 6], [2, 5, 7], [9, 8, 2], [10, 2, 6]] The extracted elements : [6, 7, 6]


