以下代码清晰解释了extend和append的区别及使用场景.
extend
a = [1, 2, 3]
a.extend([4, 5, 6])
print(a)
>>> output: [1, 2, 3, 4, 5, 6]
append
a = [1, 2, 3]
a.append([4, 5, 6])
print(a)
>>> [1, 2, 3, [4, 5, 6]]
以下代码清晰解释了extend和append的区别及使用场景.
extend
a = [1, 2, 3]
a.extend([4, 5, 6])
print(a)
>>> output: [1, 2, 3, 4, 5, 6]
append
a = [1, 2, 3]
a.append([4, 5, 6])
print(a)
>>> [1, 2, 3, [4, 5, 6]]