Mylist = list () vs mylist = [] in Python

Possible duplicate:
Listing in Python - something sneaky? Creating an empty list in Python

Consider the following:

mylist = list()

and

mylist = []

Is there any benefit to use, list()or []should I use another in a particular situation?

+5
source share
2 answers

For an empty list, I would recommend using []. This will be faster as it avoids finding a name for the inline name list. The embedded name can also be overwritten with a global or local name; it will only affect list(), not [].

list() :

a = (1, 2, 3)
b = list(a)

(Python 2.7.3rc2, Intel Core 2 Duo):

In [1]: %timeit []
10000000 loops, best of 3: 35 ns per loop

In [2]: %timeit list()
10000000 loops, best of 3: 145 ns per loop
+16

, , list . , [], list .

, [] , . , .

, list , , , .

+4

All Articles