1. Collection initialization

When initializing empty Python collections such as lists and dictionaries, favor using the corresponding literal delimiters instead of the corresponding functions.

1.1. Don’t do this

1names = list() # initialize list
2data = dict()  # initialize dictionary

1.2. Do this

1names = [] # initialize list
2data = {}  # initialize dictionary

Using the literal delimiters [] and {} is faster and more Pythonic than using list() and dictionary(). The former approach is faster since the bytecode generated does not try to find the names of the function. You should inspect the bytecode generated.

The bytecode generated for [] can be generated as follows.

1from dis import dis
2
3dis('[]')

And the bytecode will look as follows. Notice there are only 2 lines of bytecode.

11           0 BUILD_LIST               0
2            2 RETURN_VALUE

The bytecode generated for list() can be generated as follows.

1from dis import dis
2
3dis('list()')

And the bytecode will look as follows. Notice there are 3 lines of bytecode.

11           0 LOAD_NAME                0 (list)
2            2 CALL_FUNCTION            0
3            4 RETURN_VALUE

Here’s the bytecode for {}.

11           0 BUILD_MAP                0
2            2 RETURN_VALUE

Here’s the bytecode for dict().

11           0 LOAD_NAME                0 (dict)
2            2 CALL_FUNCTION            0
3            4 RETURN_VALUE