I want to pretty-print the key-value pairs of a dictionary. I have the following code:
JavaScript
x
5
1
dict = {'AAAAA': 2, 'BB': 1, 'CCCCCCCCC': 85}
2
3
for k, v in dict.items():
4
print('{}........{}'.format(k, v))
5
This is what my code currently produces:
JavaScript
1
4
1
AAAAA ..2
2
BB ..1
3
CCCCCCCCC ..85
4
But I want:
JavaScript
1
4
1
AAAAA2
2
BB1
3
CCCCCCCCC ..85
4
How do I make this happen?
Advertisement
Answer
Consider using len()
to balance out the number of dots
JavaScript
1
6
1
dict = {'AAAAA': 2, 'BB': 1, 'CCCCCCCCC': 85}
2
3
total = 15
4
for k, v in dict.items():
5
print(k, "." * (total - len(k)), v, sep="")
6