python: dictionary to string, custom format?

后端 未结 5 1406
野性不改
野性不改 2020-12-23 12:48

currently I\'m displaying keys only, each in new line:

\'
\'.join(mydict)

how do I display them like key:: value

5条回答
  •  悲&欢浪女
    2020-12-23 13:51

    Go through the dict.items() iterator that will yield a key, value tuple:

    '
    '.join(['%s:: %s' % (key, value) for (key, value) in d.items()])

    Updated with modern f-string notation:

    '
    '.join([f'{key}:: {value}' for key, value in d.items()])

提交回复
热议问题