Skip to content
Related Articles

Related Articles

Improve Article

Python – Custom Columns Matrix

  • Last Updated : 03 Jul, 2020

Sometimes, while working with Python lists, we can have a problem in which we need to extract certain columns from Matrix and recreate it. This kind of problem can have applications in data domains as they use Matrix as a prominent input parameter. Let’s discuss certain ways in which this task can be performed.

Input : test_list = [[5, 4, 3, 4], [7, 6, 3, 2], [8, 3, 9, 10]], col_list = [2]
Output : [[3], [3], [9]]

 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 = [[5, 4], [6, 2], [8, 3]], col_list = [1]
Output : [[4], [2], [3]]



Method #1 : Using list comprehension
This offers one of the ways to solve this problem. In this, we perform extraction of selective columns using nested list comprehension.




# Python3 code to demonstrate working of 
# Custom Columns Matrix
# Using list comprehension
  
# initializing list
test_list = [[5, 4, 3, 4],
             [7, 6, 3, 2], 
             [8, 3, 9, 10]]
  
# printing original list 
print("The original list : " + str(test_list))
  
# initializing Columns list 
col_list = [1, 3]
  
# Custom Columns Matrix
# Using list comprehension
res = [[sub[idx] for idx in col_list] for sub in test_list]
  
# printing result 
print("Matrix after filtering : " + str(res))
Output :
The original list : [[5, 4, 3, 4], [7, 6, 3, 2], [8, 3, 9, 10]]
Matrix after filtering : [[4, 4], [6, 2], [3, 10]]

 

Method #2 : Using itemgetter() + list comprehension
The combination of above functions can be used to solve this problem. In this, we perform the task of getting indices using itemgetter().




# Python3 code to demonstrate working of 
# Custom Columns Matrix
# Using itemgetter() + list comprehension
from operator import itemgetter
  
# initializing list
test_list = [[5, 4, 3, 4],
             [7, 6, 3, 2], 
             [8, 3, 9, 10]]
  
# printing original list 
print("The original list : " + str(test_list))
  
# initializing Columns list 
col_list = [1, 3]
  
# Custom Columns Matrix
# Using itemgetter() + list comprehension
res = [list(itemgetter(*col_list)(ele)) for ele in test_list]
  
# printing result 
print("Matrix after filtering : " + str(res))
Output :
The original list : [[5, 4, 3, 4], [7, 6, 3, 2], [8, 3, 9, 10]]
Matrix after filtering : [[4, 4], [6, 2], [3, 10]]



My Personal Notes arrow_drop_up
Recommended Articles
Page :