如何为使用生成器参数构造的dict()添加额外的键值对?

tna*_*dek 8 python syntax

可以使用生成器创建字典(PEP-289):

dict((h,h*2) for h in range(5))
#{0: 0, 1: 2, 2: 4, 3: 6, 4: 8}
Run Code Online (Sandbox Code Playgroud)

在语法上可以在同一个dict()调用中添加一些额外的键值对吗?以下语法不正确,但更好地解释了我的问题:

dict((h,h*2) for h in range(5), {'foo':'bar'})
#SyntaxError: Generator expression must be parenthesized if not sole argument
Run Code Online (Sandbox Code Playgroud)

换句话说,是否可以在单个dict()调用中构建以下内容:

{0: 0, 1: 2, 2: 4, 3: 6, 4: 8, 'foo': 'bar' }
Run Code Online (Sandbox Code Playgroud)

nin*_*cko 16

构造函数:

dict(iterableOfKeyValuePairs, **dictOfKeyValuePairs)
Run Code Online (Sandbox Code Playgroud)

例:

>>> dict(((h,h*2) for h in range(5)), foo='foo', **{'bar':'bar'})
{0: 0, 1: 2, 2: 4, 3: 6, 4: 8, 'foo': 'foo', 'bar': 'bar'}
Run Code Online (Sandbox Code Playgroud)

(请注意,如果不是唯一的参数,则需要将生成器表达式括起来.)