#append和extend的区别
原问题地址:http://stackoverflow.com/questions/252703/python-append-vs-extend
##问题
列表的两个方法append()
和extend()
的区别是什么?
##答案:
append:在末尾添加对象
x = [1, 2, 3]
x.append([4, 5])
print (x)
得出:[1, 2, 3, [4, 5]]
extend:通过添加可迭代对象的元素来扩展列表
x = [1, 2, 3]
x.extend([4, 5])
print (x)
得出: [1, 2, 3, 4, 5]