python中append和extend的区别
'''
append只能单个添加元素,extend能整体添加,但只能添加可迭代目标
'''
list1 = [1, 2, 3, 4]
list2 = [2, 3, 4, 5]
l_append = list1.append(1)
print(1_append)
>>> a
[1, 2, 3, 4, 1]
print(list2.extend(1))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable
>>> print(list2.extend([1,2,3,4]))
[1, 2, 3, 4, 1, 2, 3,4]