Creating an empty list in Python

前端 未结 5 1409
长情又很酷
长情又很酷 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 17:53

    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.

提交回复
热议问题