list.append(obj)
- obj – 添加到列表末尾的对象。
list.extend(seq)
- seq – 元素列表。在已存在的列表中添加新的列表内容
#append
lst1 = [0]
lst1.append([1,2,3])
lst1
Output: [0, [1, 2, 3]]
#extend
lst2 = [0]
lst2.extend([1,2,3])
lst2
Output: [0, 1, 2, 3]
注意:
在list.extend(seq)
中,sequence的各种形式:
lst = [0]
lst.extend((1,2,3))
Output: [0, 1, 2, 3]
lst = [0]
lst.extend('hello')
Output: [0, 'h', 'e', 'l', 'l', 'o']
lst = [0]
lst.extend(range(5))
Output:[0, 0, 1, 2, 3, 4]
可以把extend()的括号看成list()