Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Transform a Dictionary into a list of lists

Basically, I have a dictionary that I want to transform into a list of lists (with each component list consisting of the key and value from the dictionary).

The reason I am doing this is so that I can iterate through this new list with a for loop and do something with both the key and the value. If there is an easier way to do this, I am open to suggestions.

like image 930
Spencer Avatar asked Sep 15 '11 19:09

Spencer


1 Answers

How about this solution ? No need to make your hand dirty by unnecessary looping through, cleaner and shorter !!!

d = { 'a': 1, 'b': 2, 'c': 3 }
list(map(list, d.items()))
[['a', 1], ['c', 3], ['b', 2]]
like image 63
Gurucharan M K Avatar answered Oct 15 '22 01:10

Gurucharan M K