append添加单个元素 extend添加可迭代对象
append
>>> append_list = []
>>> append_list.append(1)
>>> append_list.append('a')
>>> append_list.append(['ssss'])
>>> append_list
[1, 'a', ['ssss']]
extend
>>> extend_list = []
>>> extend_list.extend(1)
Traceback (most recent call last):
TypeError: 'int' object is not iterable
>>> extend_list.extend([1])
>>> extend_list.extend(["a", [3, 4] ])
>>> extend_list
[1, 'a', [3, 4]]
itertools.chain()可以接受一个可迭代对象列表作为输入,并返回一个迭代器,有效的屏蔽掉在多个容器中迭代细节
>>> from itertools import chain
>>> a = [1, 2, 3, 4]
>>> b = ['x', 'y', 'z']
>>> for x in chain(a, b):
... print(x)
...
1 2 3 4 x y z