Use dict comprehension instead of a dict() constructor
The dictionary comprehension syntax is available on all supported Python
versions. Dictionary comprehension is modern Python syntax and is always
faster than the dict() constructor, e.g.:
$ python3 -m timeit 'dict((a, a) for a in range(20))'
100000 loops, best of 3: 2.16 usec per loop
$ python3 -m timeit '{a: a for a in range(20)}'
1000000 loops, best of 3: 0.953 usec per loop
Loading
Please register or sign in to comment