想把多个列表连接起来可以用+的方式,于是也可以用sum
List1 = [1,2,3]
List2 = [4,5,6]
merge_list = sum([List1,List2],[])
merge_list
>>>[1,2,3,4,5,6]
用sum的好处是如果有大量列表需要合并,可以不用for循环,也不用重新定义空列表。
Lists2merge = [[1,2] for _ in range(10)]
Lists2merge
>>>[[1, 2],
[1, 2],
[1, 2],
[1, 2],
[1, 2],
[1, 2],
[1, 2],
[1, 2],
[1, 2],
[1, 2]]
merge_list = sum(Lists2merge,[])
merge_list
>>>[1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2]