dazai
dazai

Reputation: 716

How do I separate a dictionary's values so that they can be printed in different columns?

I have a dictionary in which the keys have two values. I need to print the dictionary in a table format, with each value in a different column. However, when I do it, the values are together in the same column.

This is the code:

mydic = {'1': [22, 23], '2': [33,24], '3': [44,25]}

print ("{:<10} {:<10} {:<10}".format('Number', 'Total', 'Percentage'))
    
for k, v in mydic.items():
    print(f"{k:11}{v}")

This prints this:

Number      Total       Percentage
1          [22, 23]
2          [33, 24]
3          [44, 25]

I want this:

Number      Total       Percentage
1          22            23
2          33            24
3          44            25

How do I separate the values so that they can go into their respective columns instead?

Upvotes: 0

Views: 35

Answers (1)

XtianP
XtianP

Reputation: 389

Like this ?

print ("{:<10} {:<10} {:<10}".format('Number', 'Total', 'Percentage'))
for k, v in mydic.items():
    print(f"{k:11}{v[0]}{v[1]:11}")

Upvotes: 1

Related Questions