What’s the difference between list() and [] [duplicate]

One’s a function call, and one’s a literal:

>>> import dis
>>> def f1(): return list()
... 
>>> def f2(): return []
... 
>>> dis.dis(f1)
  1           0 LOAD_GLOBAL              0 (list)
              3 CALL_FUNCTION            0
              6 RETURN_VALUE        
>>> dis.dis(f2)
  1           0 BUILD_LIST               0
              3 RETURN_VALUE        

Use the second form. It’s more Pythonic, and it’s probably faster (since it doesn’t involve loading and calling a separate funciton).

Leave a Comment