在python中,向列表中添加新元素有extend和append這兩種方法,但是這兩種方法究竟有什么區(qū)別和聯(lián)系呢?
首先看extend的作用:
The extend() method adds all the elements of an iterable (list, tuple, string etc.) to the end of the list.
舉個例子:
list1.extend(iterable)
是把iterable中所有的的元素都加到了list1的末尾
而append:
The append() method adds an item to the end of the list.
list.append(item)
會將item這一個元素添加到list中
此外
a=[1,2]
b=[3,4]
c=a+b
#>>>c=[1,2,3,4]
這就是python中特殊的list擴(kuò)充方法了