I would like to print a nested dictionary in a certain format (see the expected output below) using .format()
.
It should be:
- sorted alphabetically (by name, which I managed to do)
- the column width should be the width of the longest item plus 3 spaces (which I didn't manage to do)
The dictionary in question:
data = {"Kevin H": {"street":"Maple Street 13", "phone": "01234567"},
"Stewart Bob": {"street":"Agave Street 124", "phone": "76543210"},
"John Paul": {"street":"Old Town Road 1", "phone": "09876543"}}
Expected output:
John Paul Old Town Road 1 09876543
Kevin H Maple Street 13 01234567
Stewart Bob Agave Street 124 76543210
So far I have:
sorted_data = dict(sorted(data.items()))
for name, info in sorted_data.items():
print("{} {} {}".format(name, info["street"],info["phone"]))
So, my question: how do I make the width of each column the length of the longest string (in that column) plus 3 spaces?