python - Dictionary to list with lists inside and print to csv -


i have dictionary wish turn list containing lists can write csv, ever do, doesn't work.

i used sorted(dllist.items()) sort them

[(key1, value1), (key2, value2), ... ,(keyn, valuen)] 

this have,

dict = [('aaa', [5787, 40, 1161, 1222]),   ('aab', [6103, 69, 810, 907]),   ('aac', [3081, 41, 559, 638]),   ('aae', [1011000, 191, 411, 430])] 

i want

list = [(aaa, 5787, 40, 1161, 1222),  ('aab',6103, 69, 810, 907),  ('aac', 3081, 41, 559, 638),   ('aae', 1011000, 191, 411, 430)] 

what doing wrong? , how do easiest store in csv row each element?

given assumption dictionary looks like, should conversion want , write output csv file:

import csv  d = { 'aaa': [5787, 40, 1161, 1222], 'aab': [6103, 69, 810, 907] } rows = [[k] + v k, v in sorted(d.items())]  open("out.csv", "w") out:     writer = csv.writer(out)     row in rows:         writer.writerow(row)  # out.csv: # aaa,5787,40,1161,1222 # aab,6103,69,810,907 

Comments