1.append
a = [1,2,3]
b=[1,2]
a.append(b)
-------------------------------------------------
a
Out[60]: [1, 2, 3, [1, 2]]
将b作为整个元素append进去。
2.extend
a = [1,2,3]
b=[1,2]
a.extend(b)
-----------------------------------------------
a
Out[66]: [1, 2, 3, 1, 2]
将列表b中的元素分别append进去。
当内容为数值时,只能用append。
a = [1,2,3]
a.append(3)
--------------------------------------------
a
Out[77]: [1, 2, 3, 3]
--------------------------------------------
a.extend(3)
Traceback (most recent call last):
File "C:\local\Anaconda3-4.1.1-Windows-x86_64\lib\site-packages\IPython\core\interactiveshell.py", line 2885, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-78-0218884a1728>", line 1, in <module>
a.extend(3)
TypeError: 'int' object is not iterable
-------------------------------------------
a.extend([3])
-------------------------------------------
a
Out[80]: [1, 2, 3, 3, 3]