0

In Python, what is the difference in these declarations…

my_list = []
my_list = list()

…and in these?

my_dict = {}
my_dict = dict()

Are they interpreted the same, and why would you use one over the other? I haven't seen or noticed a difference.

2
  • The only difference is that [] and {} are part of the language definition, so calling list or dict 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"] and list("abc") are very different. May 26 '16 at 17:13
  • Ah, interesting, I just tried it. Good to know, thanks! Normally I just initialise the list and then append or insert.
    – mxplusb
    May 26 '16 at 17:28
4

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.)

1
  • Huh! Good to know, that makes a lot of sense.
    – mxplusb
    May 26 '16 at 17:26
0

I almost never use the collection literals for empty collections in my Python code and prefer the named list, set, dict and tuple constructors instead.

The reason for this is that the literals can be quite confusing. For example, {} is not the empty set but an empty dictionary which is contrary to what anybody with a background in mathematics would intuitively expect. Also, the empty tuple () looks really awkward. Once I'm using the named constructors for sets, dictionaries and tuples, it's only a matter of consistency to use them for lists as well, even though the empty list literal [] looks decently and I'm not aware of any bad surprises that its usage might cause.

2
  • 2
    Unless you're doing it in a tight loop there's not much difference, but any Python programmer should be able to tell you that a {} is an empty dict, not an empty set, in spite of set literals (e.g. {'a', 'b', 'c'}). And one of the principles that I've seen used by, especially Python, experts is that you should code as if the person coming after you knows Python better than you, but they aren't as familiar with the context. That's usually going to be future you, but also others, and that's the principle I code by as well.
    – Aaron Hall
    May 27 '16 at 1:48
  • First of all, you'll have problems if you try to create a list of strings by feeding in the first string: x=list() followed by x.append("foo") yields ["foo"]; however, x=list("foo") yields ["f","o","o"]. This is similar to any iterable. Secondly, it's way slower due to the overhead of the function lookup table and cost of the function call. What else? It doesn't behave like a constructor in other languages: z=list().append("foo") yields None -- You actually have to create the list in a separate call. I do notice that z=list()+["foo"] works fine and yields ["foo"].
    – ingyhere
    Nov 21 '19 at 4:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.