This rule raises an issue when dict(), list(), or tuple() are used to create empty collections or to
initialize dictionaries with keyword arguments.
Python provides concise literal syntax for creating empty dictionaries ({}), lists ([]), and tuples (()). It
also offers a direct literal syntax for initializing dictionaries with key-value pairs (e.g., {'a': 1, 'b': 2}).
Using the function calls dict(), list(), and tuple() for these purposes is generally less preferred for a
few reasons:
dict, list, or tuple in the
current scope. While often negligible for single calls, using literals is a direct instruction to the interpreter and can be marginally faster,
especially in performance-sensitive code or tight loops. Specifically, the following patterns are discouraged:
dict() to create an empty dictionary instead of {}. list() to create an empty list instead of []. tuple() to create an empty tuple instead of (). dict(key='value', …) to initialize a dictionary instead of {'key': 'value', …} when keys are simple strings
valid as identifiers. While the functional difference is minimal for creating empty collections or simple dictionaries, adopting literals promotes a more direct and idiomatic coding style.
To fix this replace the function calls dict(), list(), and tuple() with their equivalent literal syntax.
dict() with {}. list() with []. tuple() with (). dict(key1=value1,
key2=value2) with {'key1': value1, 'key2': value2}. empty_d = dict() # Noncompliant: the dict constructor is used instead of the literal syntax
empty_d = {} # Compliant