简体   繁体   中英

Merging dict to into a list of lists

I have a dict that like this

{'id':[0, 1, 2, 3, 4, 5],
 'code':['A1', 'A2', 'A3', 'A4', 'A5', 'A6'],
 'price':['35', '23', '54', '17', '68', '199'] 
}


How can I make it look like this:

data= [
    
   [0, 'A1', '35'],
   [1, 'A2', '23'],
   [2, 'A3', '54'],
   [3, 'A4', '17'],
   [4, 'A5', '68'],
   [5, 'A6', '199'],

]

I know that I can do it in python but just want someone to point me in the direction of how to do it, because I don't know where to start

You can use a transpose operation -- note that zip() returns an iterable of tuples, so we need to transform them to lists using map() :

list(map(list, zip(*data.values())))

This outputs:

[[0, 'A1', '35'], [1, 'A2', '23'], [2, 'A3', '54'], [3, 'A4', '17'], [4, 'A5', '68'], [5, 'A6', '199']]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM