Given a dictionary
{
'a': [1, 2, 3],
'b': [4, 5, 6]
}
how do I get the output
[
['a', 1, 2, 3],
['b', 4, 5, 6]
]
Given a dictionary
{
'a': [1, 2, 3],
'b': [4, 5, 6]
}
how do I get the output
[
['a', 1, 2, 3],
['b', 4, 5, 6]
]
Do you mean:
>>> a = {
... 'a':[1,2,3],
... 'b':[4,5,6]
... }
>>> [[key]+value for key, value in a.items()]
[['b', 4, 5, 6], ['a', 1, 2, 3]]
If you need sorted:
>>> sorted([[key]+value for key, value in a.items()])
[['a', 1, 2, 3], ['b', 4, 5, 6]]
>>>
This solution should be closer to what you want (key and values in one list per dictionary item):
d = {
'a':[1,2,3],
'b':[4,5,6]
}
print sorted([[key] + values for key, values in d.iteritems()])
Output:
[['a', 1, 2, 3], ['b', 4, 5, 6]]
You need to use OrderedDict if you want to get a specific order.
If you want to sort according to the keys :
D = {'a':[1,2,3],'b':[4,5,6]}
[[k] + D[k] for k in sorted(D.keys())]