Dict-Literal¶
Sourcery rule id: dict-literal
¶
Description¶
Replace dict()
with {}
Before¶
x = dict()
After¶
x = {}
Explanation¶
The most concise and Pythonic way to create a dictionary is to use the {}
notation.
This fits in with the way we create dictionaries with items, saving a bit of mental energy that might be taken up with thinking about two different ways of creating dicts.
x = {"first": "thing"}
Doing things this way has the added advantage of being a nice little performance improvement.
Here are the timings before and after the change:
$ python3 -m timeit "x = dict()"
5000000 loops, best of 5: 69.8 nsec per loop
$ python3 -m timeit "x = {}"
20000000 loops, best of 5: 29.4 nsec per loop
Similar reasoning and performance results hold for replacing list()
with []
.