Grouping Dictionaries
The Problem
Sometimes, rather being a list of records, the data you’d like to iterate is a dictionary:
{
"today": {
"event": "Bought sausages"
},
"yesterday": {
"event": "Bought a train set"
}
}
To create a table with When and Event columns, we’ll use a specific dictionary grouper.
Code Sample
Note that the column set is grouped via ByKey
. The path to the key name is read from ByKey.key()
and a path that drills into the key’s value is built with the ByKey.value()
function.
from rolumns import ByKey, Columns
from rolumns.renderers import RowsRenderer
data = {
"today": {
"event": "Bought sausages",
},
"yesterday": {
"event": "Bought a train set",
},
}
columns = Columns(ByKey())
columns.add("When", ByKey.key())
columns.add("Event", ByKey.value("event"))
renderer = RowsRenderer(columns)
rows = renderer.render(data)
print(list(rows))
Result
[['When', 'Event'],
['today', 'Bought sausages'],
['yesterday', 'Bought a train set']]