Change language

Python | Convert column to separate list items of lists

|

Method # 1: Using List Slicing and List Comprehension
The slicing and list comprehension functionality can be combined to accomplish the specific task of extracting a column from a list and then added as a new item using a list comprehension.

# Python3 code for demonstration
# column for separating elements into list of lists
# using list slicing and list comprehension

 
# list list initialization

test_list = [[ 1 , 3 , 4 ],

[ 6 , 2 , 8 ],

[ 9 , 10 , 5 ]]

 
# print original list

print ( "The original list is:" + str (test_list))

  
# using list slicing and list comprehension # column to separate items in the list of lists

res = [i for nest_list in [[j [ 1 :], [j [ 0 ]]]

for j in test_list] for i in nest_list]

  
# print 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], [1], [2, 8], [6], [10, 5], [9]] 

Method # 2: Using itertools.chain () + list comprehension + list slicing
The above method can be improved by creating chaining concepts and reducing the cost of list comprehension and reducing the time required to complete that particular task.

# Python3 demo code
# column for separating items in the list of lists
# using itertools.chain () + list comprehension + list slicing

from itertools import chain

 
# list list initialization

test_list = [[[ 1 , 3 , 4 ],

[ 6 , 2 , 8 ],

[ 9 , 10 , 5 ]]

 
# print original list

print ( "The original list is:" + str (test_list))

  
# using itertools.chain () + list comprehension + list slicing
# column for separating items in the list of lists

res = list (chain ( * [ list ((sub [ 1 :], [ sub [ 0 ]]))

  for sub in test_list]))

 
# print 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], [1], [2, 8], [6], [ 10, 5], [9]] 

Python | Convert column to separate list items of lists Python functions: Questions

Python | Convert column to separate list items of lists sep: Questions

Shop

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method