Reputation: 351
Suppose I have a dictionary like so:
{'a':['data', 1, 2, 3],
'b':['data2', 4, 3, 2, 1, 0],
'c':['data3', 3, 4, 5, 6]}
And I wanted to to be compressed likeso:
{'letters':['a','b','c'],
'values':[['data', 1, 2, 3], ['data2', 4, 3, 2, 1, 0], ['data3', 3, 4, 5, 6]]}
Where letters
and values
can be any arbitrary names set for the key.
Upvotes: 0
Views: 43
Reputation: 42143
You could use zip to combine your key names with the content of the dictionary items:
d = {'a':['data', 1, 2, 3],
'b':['data2', 4, 3, 2, 1, 0],
'c':['data3', 3, 4, 5, 6]}
d = dict(zip(('letters','values'),zip(*d.items())))
print(d)
{'letters': ('a', 'b', 'c'),
'values': (['data', 1, 2, 3],['data2', 4, 3, 2, 1, 0],['data3', 3, 4, 5, 6])}
Upvotes: 1
Reputation: 260410
Assuming:
d = {'a':['data', 1, 2, 3],
'b':['data2', 4, 3, 2, 1, 0],
'c':['data3', 3, 4, 5, 6]}
you can use:
out = {'letters': list(d.keys()),
'values': list(d.values())
}
output:
>>> out
{'letters': ['a', 'b', 'c'],
'values': [['data', 1, 2, 3], ['data2', 4, 3, 2, 1, 0], ['data3', 3, 4, 5, 6]]
}
Upvotes: 3