Transform dict values to key,value

时间:2019-04-17 00:06:10

标签: python

I have values in dict_values format. How do I turn this into a new dictionary as corresponding values?

I'd like first value to act as key, and second value as act as value.

    for d in request.data['names']:
        print(d.values())

dict_values(['jack', '1'])
dict_values(['sam', '2'])
dict_values(['ryan', '3'])
dict_values(['we', '4'])
dict_values(['jam', '5'])
dict_values(['pam', '6'])

to

{'jack': '1'}
{'sam': '2'}
{'ryan': '3'}
{'we': '4'}
{'jam': '5'}
{'pam': '6'}

2 个答案:

答案 0 :(得分:0)

Try this:

    for d in request.data['names']:
        print(dict([d.values()]))

答案 1 :(得分:0)

dic = {}
for d in request.data['names']:
    dic[d[0]] = d[1]