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]

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Extract ith column values from jth column values
# Using loop
  
# initializing list
test_list = [[4, 5, 6], [2, 5, 7], [9, 8, 2], [10, 2, 6]]
  
# printing original list
print("The original list is : " + str(test_list))
  
# initializing list 
search_list = [5, 2]
  
# initializing search index 
search_idx = 1
  
# initializing extract index
ext_idx = 2
  
# Extract ith column values from jth column values
# Using loop
res = []
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)) 

chevron_right


Output :



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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Extract ith column values from jth column values
# Using set() + list comprehension
  
# initializing list
test_list = [[4, 5, 6], [2, 5, 7], [9, 8, 2], [10, 2, 6]]
  
# printing original list
print("The original list is : " + str(test_list))
  
# initializing list 
search_list = [5, 2]
  
# initializing search index 
search_idx = 1
  
# initializing extract index
ext_idx = 2
  
# Extract ith column values from jth column values
# Using set() + list comprehension
temp = 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)) 

chevron_right


Output :

The original list is : [[4, 5, 6], [2, 5, 7], [9, 8, 2], [10, 2, 6]]
The extracted elements : [6, 7, 6]



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.