August Williams
August Williams

Reputation: 929

How to print a dictionary with keys on identical columns each time?

I am trying to print multiple dictionaries on separate lines, that contain the same keys, but different values each time. The values can be different in a way that each time the dictionary is printed, it can appear messy; because the keys are in different places every time. For example:

{0: 10, 1: 5, 2: 2, 3: 4, 4: 1, 5: 1, 6: 0, 7: 0, 8: 0, 9: 0, 10: 1}
{0: 309, 1: 156, 2: 76, 3: 36, 4: 22, 5: 6, 6: 4, 7: 5, 8: 1, 9: 0, 10: 1}
{0: 478, 1: 216, 2: 125, 3: 57, 4: 37, 5: 9, 6: 2, 7: 2, 8: 2, 9: 0, 10: 1}
{0: 1287, 1: 717, 2: 332, 3: 165, 4: 94, 5: 26, 6: 15, 7: 9, 8: 8, 9: 4, 10: 1}
{0: 104, 1: 53, 2: 24, 3: 17, 4: 6, 5: 4, 6: 0, 7: 1, 8: 0, 9: 1, 10: 1}

How can I print dictionaries in a way that the keys will be on the same column, each time the dictionary is printed? This way, the dictionary will be of equal length every time, and appear neater as a result.

Upvotes: 0

Views: 205

Answers (2)

Moinuddin Quadri
Moinuddin Quadri

Reputation: 48067

Since dict are un-ordered in nature, you cannot print the ordered content. In order to achieve this, you may sort the content of dict based on keys and generate the string similar to repr of dictionary. For example:

for item in my_list:
    print('{%s}' % ', '.join('%s: %s' % (k, v) for k, v in sorted(item.items())))

# which prints:
#   {0: 10, 1: 5, 2: 2, 3: 4, 4: 1, 5: 1, 6: 0, 7: 0, 8: 0, 9: 0, 10: 1}
#   {0: 309, 1: 156, 2: 76, 3: 36, 4: 22, 5: 6, 6: 4, 7: 5, 8: 1, 9: 0, 10: 1}
#   {0: 478, 1: 216, 2: 125, 3: 57, 4: 37, 5: 9, 6: 2, 7: 2, 8: 2, 9: 0, 10: 1}
#   {0: 1287, 1: 717, 2: 332, 3: 165, 4: 94, 5: 26, 6: 15, 7: 9, 8: 8, 9: 4, 10: 1}
#   {0: 104, 1: 53, 2: 24, 3: 17, 4: 6, 5: 4, 6: 0, 7: 1, 8: 0, 9: 1, 10: 1}

where my_list is the list of dictas:

my_list = [{0: 10, 1: 5, 2: 2, 3: 4, 4: 1, 5: 1, 6: 0, 7: 0, 8: 0, 9: 0, 10: 1},
           {0: 309, 1: 156, 2: 76, 3: 36, 4: 22, 5: 6, 6: 4, 7: 5, 8: 1, 9: 0, 10: 1},
           {0: 478, 1: 216, 2: 125, 3: 57, 4: 37, 5: 9, 6: 2, 7: 2, 8: 2, 9: 0, 10: 1},
           {0: 1287, 1: 717, 2: 332, 3: 165, 4: 94, 5: 26, 6: 15, 7: 9, 8: 8, 9: 4, 10: 1},
           {0: 104, 1: 53, 2: 24, 3: 17, 4: 6, 5: 4, 6: 0, 7: 1, 8: 0, 9: 1, 10: 1}
          ]

Upvotes: 2

Prune
Prune

Reputation: 77837

One way is to learn a data-frame interface, such as PANDAS, so you can drop the values into labeled columns. This is likely the best way.

If you need to do this in Python with your own coding, I suggest that you write a pretty-printer that takes each element in order, prints it in a nicely formatted column (say, lead with a \t character), and adds the braces by brute force. For instance:

print '{',
for key in range(key_min, key_max+1):
    print key, ':', dict[key], ',\t',
print '}'

That does one row. Is this satisfactory? You could also use a formatted print for the entire line; I'm not sure how much time you want to spend on learning output formatting.

Upvotes: 1

Related Questions