255

What is the best way to create a new empty list in Python?

l = [] 

or

l = list()

I am asking this because of two reasons:

  1. Technical reasons, as to which is faster. (creating a class causes overhead?)
  2. Code readability - which one is the standard convention.
suci
  • 178
  • 7
user225312
  • 126,773
  • 69
  • 172
  • 181

5 Answers5

351

Here is how you can test which piece of code is faster:

% python -mtimeit  "l=[]"
10000000 loops, best of 3: 0.0711 usec per loop

% python -mtimeit  "l=list()"
1000000 loops, best of 3: 0.297 usec per loop

However, in practice, this initialization is most likely an extremely small part of your program, so worrying about this is probably wrong-headed.

Readability is very subjective. I prefer [], but some very knowledgable people, like Alex Martelli, prefer list() because it is pronounceable.

Community
  • 1
  • 1
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
142

list() is inherently slower than [], because

  1. there is symbol lookup (no way for python to know in advance if you did not just redefine list to be something else!),

  2. there is function invocation,

  3. then it has to check if there was iterable argument passed (so it can create list with elements from it) ps. none in our case but there is "if" check

In most cases the speed difference won't make any practical difference though.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Nas Banov
  • 28,347
  • 6
  • 48
  • 67
  • 41
    +1: It feels good to understand why `list()` is slower than `[]`! – Eric O. Lebigot Jun 04 '10 at 08:16
  • 2
    In the case of `list()` it has only to check if there was any arg at all ... "check if iterable" and "create list with elements" just don't happen; they only apply if there is an arg. It's even possible that the C code for `[]` calls the same C code as `list()`. In any case the time involved in (c) would be tiny compared with (a) + (b). – John Machin Jun 04 '10 at 09:57
  • 1
    @John Machin - sorry for confusion, what i meant in (c) was that it will need to check if there was argument, yes. the rest was about what will happen if there were argument, which in our case there is none – Nas Banov Jun 04 '10 at 21:16
18

I use [].

  1. It's faster because the list notation is a short circuit.
  2. Creating a list with items should look about the same as creating a list without, why should there be a difference?
Georg Schölly
  • 124,188
  • 49
  • 220
  • 267
3

I do not really know about it, but it seems to me, by experience, that jpcgt is actually right. Following example: If I use following code

t = [] # implicit instantiation
t = t.append(1)

in the interpreter, then calling t gives me just "t" without any list, and if I append something else, e.g.

t = t.append(2)

I get the error "'NoneType' object has no attribute 'append'". If, however, I create the list by

t = list() # explicit instantiation

then it works fine.

Banneisen
  • 33
  • 5
Yinyue
  • 145
  • 1
  • 3
  • 12
  • 22
    It's because `t.append(1)` modifies `t` in place , it doesn't return anything but `None` and you are assigning this `None` to `t`. So `t` refers now to `None` instead of to the list. Your mistake here was to write `t=t.append(1)` instead of just `t.append(1)`. You'll notice the same behaviour with `list()`, so there`s no difference here. – Darkonaut Jan 09 '18 at 22:00
3

Just to highlight @Darkonaut answer because I think it should be more visible.

new_list = [] or new_list = list() are both fine (ignoring performance), but append() returns None, as result you can't do new_list = new_list.append(something).

clinton3141
  • 4,751
  • 3
  • 33
  • 46
oVo
  • 111
  • 1
  • 3