Python | Ways to concatenate two lists 连接两个list

Method #1: Using + operator

The most conventional method to perform the list concatenation, the use of “+” operator can easily add the whole of one list behind the other list and hence perform the concatenation.

# Python 3 code to demonstrate list 
# concatenation using + operator 

# Initializing lists 
test_list3 = [1, 4, 5, 6, 5] 
test_list4 = [3, 5, 7, 2, 5] 

# using + operator to concat 
test_list3 = test_list3 + test_list4 

# Printing concatenated list 
print ("Concatenated list using + : "
				+ str(test_list3)) 

Method #2 : Using list comprehension

List comprehension can also accomplish this task of list concatenation. In this case, a new list is created, but this method is a one liner alternative to the loop method discussed above.

# Python3 code to demonstrate list 
# concatenation using list comprehension 

# Initializing lists 
test_list1 = [1, 4, 5, 6, 5] 
test_list2 = [3, 5, 7, 2, 5] 

# using list comprehension to concat 
res_list = [y for x in [test_list1, test_list2] for y in x] 

# Printing concatenated list 
print ("Concatenated list using list comprehension: "
									+ str(res_list)) 

Method #3 : Using extend()

extend() is the function extended by lists in Python and hence can be used to perform this task. This function performs the inplace extension of first list.

# Python3 code to demonstrate list 
# concatenation using list.extend() 

# Initializing lists 
test_list3 = [1, 4, 5, 6, 5] 
test_list4 = [3, 5, 7, 2, 5] 

# using list.extend() to concat 
test_list3.extend(test_list4) 

# Printing concatenated list 
print ("Concatenated list using list.extend() : "
							+ str(test_list3)) 

Method #4 : Using * operator

Using * operator, this method is the new addition to list concatenation and works only in Python 3.6+. Any no. of lists can be concatenated and returned in a new list using this operator.

# Python3 code to demonstrate list 
# concatenation using * operator 

# Initializing lists 
test_list1 = [1, 4, 5, 6, 5] 
test_list2 = [3, 5, 7, 2, 5] 

# using * operator to concat 
res_list = [*test_list1, *test_list2] 

# Printing concatenated list 
print ("Concatenated list using * operator : "
							+ str(res_list)) 

Method #5 : Using itertools.chain()

itertools.chain() returns the iterable after chaining its arguments in one and hence does not require to store the concatenated list if only its initial iteration is required. This is useful when concatenated list has to be used just once.

# Python3 code to demonstrate list 
# concatenation using itertools.chain() 
import itertools 

# Initializing lists 
test_list1 = [1, 4, 5, 6, 5] 
test_list2 = [3, 5, 7, 2, 5] 

# using itertools.chain() to concat 
res_list = list(itertools.chain(test_list1, test_list2)) 

# Printing concatenated list 
print ("Concatenated list using itertools.chain() : "
									+ str(res_list)) 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值