Pythonic Name-Value与字典转换配对

Whi*_*ger 0 python python-3.x

给定一组名称 - 值对的dicts,将它们转换为字典的最有效或最Pythonic方法是什么,名称为键,值为值?

这就是我的想法.它很短,似乎工作正常,但有没有一些内置函数来做这种事情?

verbose_attributes = [
    {
        'Name': 'id',
        'Value': 'd3f23fa5'
    },
    {
        'Name': 'first_name',
        'Value': 'Guido'
    },
    {
        'Name': 'last_name',
        'Value': 'van Rossum'
    }]

attributes = {}

for pair in verbose_attributes:
    attributes[pair['Name']] = pair['Value']

print(repr(attributes))
# {'id': 'd3f23fa5', 'first_name': 'Guido', 'last_name': 'van Rossum'}
Run Code Online (Sandbox Code Playgroud)

简而言之,是否有更好的转换verbose_attributes方式attributes

Aus*_*tin 7

使用字典理解:

attributes = {x['Name']: x['Value'] for x in verbose_attributes}
Run Code Online (Sandbox Code Playgroud)