Skip to content

Latest commit

 

History

History
25 lines (15 loc) · 592 Bytes

104.md

File metadata and controls

25 lines (15 loc) · 592 Bytes

#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]