Creating an empty list in Python

前端 未结 5 1395
长情又很酷
长情又很酷 2020-11-28 17:17

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

l = [] 

or

l = list()

I am asking this because

5条回答
  •  一整个雨季
    2020-11-28 18:05

    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.

提交回复
热议问题