Python | Ways to combine two lists

Python Methods and Functions

Method # 1: Using the naive method

In this method, we traverse the second list and continue to add elements to the first list so that the first list contains all the elements in both lists and, hence doing the addition.

# Python3 code to show the list
# concatenate using naive method

 
# Initializing lists

test_list1 = [ 1 , 4 , 5 , 6 , 5 ]

test_list2 = [ 3 , 5 , 7 , 2 , 5 ]

  
# using a naive method to concatenate

for i in test_list2:

test_list1.append (i)

  
# Print multiple list

print ( "Concatenated list using naive meth od: " 

  + str (test_list1))

Exit :

 Concatenated list using naive method: [1, 4, 5, 6, 5, 3, 5, 7, 2, 5] 

Method # 2: Using the + Operator

The most common method for doing list concatenation, using the "+" operator makes it easy to add an entire list after another list and therefore do the concatenation.

# Python 3 code to show the list
# join using the + operator

 
# Initializing lists

test_list3 = [ 1 , 4 , 5 , 6 , 5 ]

test_list4 = [ 3 , 5 , 7 , 2 , 5 ]

 
# using the + operator to concatenate

test_list3 = test_list3 + test_list4

 
# Print multiple list

print ( "Concatenated list using +:"

+ str (test_list3 ))

Exit:

 Concatenated list using +: [1, 4, 5, 6, 5, 3, 5, 7, 2, 5] 

Method # 3: Using comprehension list

List comprehension can also accomplish this task of combining lists. In this case, a new list is created, but this method is an alternative to the one-line method described above.

# Python3 code to show the list
# merge using a list comprehension

 
# Initializing lists

test_list1 = [ 1 , 4 , 5 , 6 , 5 ]

test_list2 = [ 3 , 5 , 7 , 2 , 5 ]

 
# using list comprehension for concatat

res_list = [y for x in [test_list1, test_list2] for y in x]

 
# Print composite list

print ( "Concatenated list using list c omprehension: "

  + str (res_list))

Exit :

 Concatenated list using list comprehension: [1, 4, 5, 6, 5, 3, 5, 7, 2, 5] 

Method # 4: Using extend()

extended extend () — it is a function that is extensible with lists in Python and therefore can be used to accomplish this task. This function expands the first list in place.

# Python3 code to showcase the list
# join using list.extend ()

 
# Initializing lists

test_list3 = [ 1 , 4 , 5 , 6 , 5 ]

test_list4 = [ 3 , , 7 , 2 , 5 ]

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

  
# Print multiple list

print ( "Concatenated list using list.extend (): "

  + str (test_list3))

Output:

 Concatenated list using list.extend (): [1, 4, 5, 6, 5, 3, 5, 7, 2, 5] 

Method # 5: Using the operator * < / p>

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

< / tr>

# Python3 code to show the list
# concatenation using the * operator

 
# Initializing lists

test_list1 = [ 1 , 4 , 5 , 6 , 5 ]

test_list2 = [ 3 , 5 , 7 , 2 , 5 ]

 
# using the * operator to concatenate

res_list = [ * test_list1, * test_list2]

 
# Print multiple list

print ( " Concatenated list using * operator: " 

+ str (res_list))

Exit:

 Concatenated list using * operator: [1, 4, 5, 6, 5 , 3, 5, 7, 2, 5] 

Method # 6: Using itertools.chain()

itertools.chain () returns an iterable after concatenating its arguments into one and therefore does not require storing the linked list if only its initial iteration is required. This is useful when the composite list is to be used only once.

# Python3 code to show the list
# chaining 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 concatenate

res_list = list (itertools.chain (test_list1, test_list2))

 
# Print multiple list

print ( "Concatenated list using itertools.chain ():"

+ str (res_ list))

Exit:

 Concatenated list using itertools.chain (): [1, 4, 5, 6, 5, 3, 5, 7, 2, 5] 




Get Solution for free from DataCamp guru