The difference between
my_list = list()
and
my_list = []
is that list
requires a namespace lookup, first in the module level globals, then in the builtins.
On the other hand, []
is a list literal and is parsed as creating a new list from the language, which doesn't require any name lookups. So the literal is faster on object creation.
Otherwise, they are both the same, for empty lists.
The same analogously applies to dict()
and {}
.
(Slightly beyond the scope of the question, but the difference, as constructors of non-empty lists, is that list()
takes an iterable to construct the list, and []
constructs the list with only the objects with which you create it, usually literals.)
[]
and{}
are part of the language definition, so callinglist
ordict
requires to load these functions (LOAD_GLOBAL
instruction), but they both have the same results. There's no practical difference between the two. Though, do note that["abc"]
andlist("abc")
are very different. – Vincent Savard May 26 '16 at 17:13