How to add extra key-value pairs to a dict() constructed with a generator argument?
python, syntax
Solution
Constructor:
dict(iterableOfKeyValuePairs, **dictOfKeyValuePairs)
Example:
>>> 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'}
(Note that you will need to parenthesize generator expressions if not the sole argument.)
Problem
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' } ```