# Python | Convert column to separate elements in list of lists

There are instances in which we might require to extract a particular column of a Matrix and assign its each value as separate entity in list and this generally has a utility in Machine Learning domain. Let’s discuss certain ways in which this action can be performed.

Method #1 : Using list slicing and list comprehension
The functionality of list slicing and comprehension can be clubbed to perform the particular task of extracting a column from a list and then it can be added as new element using list comprehension.

 `# Python3 code to demonstrate   ` `# column to separate elements in list of lists ` `# using list slicing and list comprehension ` ` `  `# initializing list of list  ` `test_list ``=` `[[``1``, ``3``, ``4``], ` `             ``[``6``, ``2``, ``8``], ` `             ``[``9``, ``10``, ``5``]] ` ` `  `# printing original list  ` `print` `(``"The original list is : "` `+` `str``(test_list)) ` ` `  `# using list slicing and list comprehension ` `# column to separate elements in list of lists ` `res ``=` `[i ``for` `nest_list ``in` `[[j[``1` `: ], [j[``0``]]] ` `         ``for` `j ``in` `test_list] ``for` `i ``in` `nest_list] ` ` `  `# printing result ` `print` `(``"The list after colum shift is : "` `+` `str``(res)) `

Output :

```The original list is : [[1, 3, 4], [6, 2, 8], [9, 10, 5]]
The list after colum shift is : [[3, 4], , [2, 8], , [10, 5], ]
```

Method #2 : Using `itertools.chain()` + list comprehension + list slicing
The above method can be improved by inducing the concept of element chaining and reduce the overhead of the list comprehension and reducing the time taken to execute this particular task.

 `# Python3 code to demonstrate   ` `# column to separate elements in list of lists ` `# using itertools.chain()+ list comprehension + list slicing ` `from` `itertools ``import` `chain ` ` `  `# initializing list of list  ` `test_list ``=` `[[``1``, ``3``, ``4``], ` `             ``[``6``, ``2``, ``8``], ` `             ``[``9``, ``10``, ``5``]] ` ` `  `# printing original list  ` `print` `(``"The original list is : "` `+` `str``(test_list)) ` ` `  `# using itertools.chain() + list comprehension + list slicing ` `# column to separate elements in list of lists ` `res ``=` `list``(chain(``*``[``list``((sub[``1``: ], [sub[``0``]])) ` `                      ``for` `sub ``in` `test_list])) ` ` `  `# printing result ` `print` `(``"The list after colum shift is : "` `+` `str``(res)) `

Output :

```The original list is : [[1, 3, 4], [6, 2, 8], [9, 10, 5]]
The list after colum shift is : [[3, 4], , [2, 8], , [10, 5], ]
```

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.