How to print a dictionary line by line in Python?

后端 未结 13 1499
情话喂你
情话喂你 2020-11-28 02:02

This is the dictionary

cars = {\'A\':{\'speed\':70,
        \'color\':2},
        \'B\':{\'speed\':60,
        \'color\':3}}

Using this

13条回答
  •  Happy的楠姐
    2020-11-28 02:30

    Check the following one-liner:

    print('\n'.join("%s\n%s" % (key1,('\n'.join("%s : %r" % (key2,val2) for (key2,val2) in val1.items()))) for (key1,val1) in cars.items()))
    

    Output:

    A
    speed : 70
    color : 2
    B
    speed : 60
    color : 3
    

提交回复
热议问题