The dict builtin will create a dictionary from keyword arguments:
>>> dict(a=1, b=2)
{'a': 1, 'b': 2}
but you can use integers as keyword arguments:
>>> dict(a=1, 2=2)
File "<stdin>", line 1
dict(a=1, 2=2)
^^
SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
However, dict
will also accept an iterable of key/value tuples, and in this case they keys may be integers
>>> dict([('a', 1), (2, 2)])
{'a': 1, 2: 2}
If your keys are the same for all dicts you can use zip:
>>> keys = ('a', 2)
>>> values = [(1, 2), (3, 4)]
>>> for vs in values:
... print(dict(zip(keys, vs)))
...
{'a': 1, 2: 2}
{'a': 3, 2: 4}
However, if your keys are not consistent, there's nothing wrong with using the literal {...}
constructor. In fact, if it's efficiency that you want, the literal constructor may be the best choice.