python列表连接
时间: 2024-09-04 21:00:44 浏览: 46
在Python中,你可以使用"+"运算符或者extend()函数将两个或更多的列表连接起来。以下是两种常见的方式:
使用"+"运算符:
list1 = [1, 2, 3] list2 = [4, 5, 6] result = list1 + list2 print(result) # 输出: [1, 2, 3, 4, 5, 6]
这里直接通过加号操作符将两个列表元素逐个对应相加。
使用extend()方法:
list1 = [1, 2, 3] list2 = [4, 5, 6] list1.extend(list2) print(list1) # 输出: [1, 2, 3, 4, 5, 6]
在这个例子中,extend()方法会将list2的所有元素追加到list1的末尾,而不是创建一个新的列表。
相关推荐










