Python 将元素追加到列表中的更快操作

Python 将元素追加到列表中的更快操作,python,python-2.7,Python,Python 2.7,我需要向列表中添加一个/多个元素,我有两个选项: mylist=mylist+newlist或(elemet) 或mylist.append(ele) 哪个会更快?mylist.append(ele)会更快。这在Python文档中有文档记录 从文件中引用- The method append() shown in the example is defined for list objects; it adds a new element at the end of the list. In th

我需要向列表中添加一个/多个元素,我有两个选项:

mylist=mylist+newlist
(elemet)

mylist.append(ele)

哪个会更快?

mylist.append(ele)
会更快。这在Python文档中有文档记录

从文件中引用-

The method append() shown in the example is defined for list objects; it adds a new element at the end of the list. In this example it is equivalent to result = result + [a], but more efficient.
myList=myList+something
必须创建一个新列表并重新分配它

比较
timeit
结果-

>>> timeit('myList = myList + ["a"]', 'myList = []', number = 50000)
11.35058911138415
>>> timeit('myList.append("a")', 'myList = []', number = 50000)
0.010776052286637139

您可以自己使用:
python-mcprofile yourscript.py
对其进行分析。发布答案后,不要删除该问题。