温馨提示×

python怎么将多个列表合并

小亿
90
2023-12-21 06:19:14
栏目: 编程语言

在Python中,可以使用"+"运算符来合并多个列表。以下是几种常用的方法:

  1. 使用"+"运算符:
list1 = [1, 2, 3]
list2 = ['a', 'b', 'c']
merged_list = list1 + list2
  1. 使用extend()方法:
list1 = [1, 2, 3]
list2 = ['a', 'b', 'c']
list1.extend(list2)
  1. 使用append()方法和循环:
list1 = [1, 2, 3]
list2 = ['a', 'b', 'c']
for item in list2:
    list1.append(item)

无论使用哪种方法,上述代码的输出结果都是 [1, 2, 3, 'a', 'b', 'c'],即将两个列表合并成一个新的列表。

0