F
F
forgoty2015-10-25 23:05:04
Python
forgoty, 2015-10-25 23:05:04

How to make pretty output of dictionaries to Python console?

>>>t={'a':123,'b':456}
>>>t
{'b': 456, 'a': 123}
I want output like this:
b: 456
a: 123
Guys, please tell me how to do it. Google didn't come up with anything good. Thanks in advance!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
K
Kirill Romanov, 2015-10-26
@forgoty

for key, value in t.items():
  print("{0}: {1}".format(key,value))

D
Dimonchik, 2015-10-25
@dimonchik2013

pprint

V
Vasily Chernenkov, 2018-12-23
@123581321345589

dic = {"name":"value"}

# Вариант 1
import json
print(json.dumps(dic, indent=4, sort_keys=True))

# Вариант 2.1
from pprint import pprint 
pprint(dic)

# Вариант 2.2
import pprint 
pprint.pprint(dic)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question