Is there a more elegant way for unpacking keys and values of a dictionary into two lists, without losing consistence?

回眸只為那壹抹淺笑 提交于 2019-12-18 12:19:23

问题


What I came up with is:

keys, values = zip(*[(key, value) for (key, value) in my_dict.iteritems()])

But I am not satisfied. What do the pythonistas say?


回答1:


What about using my_dict.keys() and my_dict.values()?

keys, values = my_dict.keys(), my_dict.values()



回答2:


keys, values = zip(*d.items())


来源:https://stackoverflow.com/questions/6612769/is-there-a-more-elegant-way-for-unpacking-keys-and-values-of-a-dictionary-into-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!