Creating an empty list in Python
Asked Answered
S

4

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.
Smatter answered 4/6, 2010 at 7:26 Comment(1)
"faster"? Why didn't you run timeit?Subsonic
L
352

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.

Lenoir answered 4/6, 2010 at 7:29 Comment(3)
Wow, thanks for the profiling. I had always wanted to know how it was done.Smatter
Why does list('') give [] instead of ['']?Autoionization
This is because this func make list from iterable types, you couldnt insert in more than 1 argument. (Try to type list('abcd') and you understand everything)Ectopia
O
143

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.

Oakley answered 4/6, 2010 at 7:59 Comment(3)
+1: It feels good to understand why list() is slower than []!Slipslop
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).Couloir
@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 noneOakley
P
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?
Physicalism answered 4/6, 2010 at 8:6 Comment(0)
K
0

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.

Kramer answered 14/3, 2017 at 18:43 Comment(2)
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.Heraclea
And to be clear, @jpcgt's deleted post (that claimed def f(lst=[]): behaved differently from def f(lst=list()): in terms of avoiding default argument aliasing) was flagrantly wrong in its claims. Every claim you've made here is either 1) Wrong, or 2) Wholly irrelevant to the question.Congruent

© 2022 - 2024 — McMap. All rights reserved.