One can create dictionaries using generators (PEP-289):
dict((h,h*2) for h in range(5))
#{0: 0, 1: 2, 2: 4, 3: 6, 4: 8}
Is it syntactically possible to add some extra key-value pairs in the same dict() call? The following syntax is incorrect but better explains my question:
dict((h,h*2) for h in range(5), {'foo':'bar'})
#SyntaxError: Generator expression must be parenthesized if not sole argument
In other words, is it possible to build the following in a single dict() call:
{0: 0, 1: 2, 2: 4, 3: 6, 4: 8, 'foo': 'bar' }
Constructor:
Example:
(Note that you will need to parenthesize generator expressions if not the sole argument.)