Let’s learn about different ways to merge the lists in python.

Merging List in Python
List:
Python knows a number of compound data types, used to group together other values. The most versatile is the list, which can be written as a list of comma-separated values (items) between square brackets. Lists might contain items of different types, but usually the items all have the same type.
Refer to my story of Python Lists
Different ways to merge list:

- append
- The append method will add an item to the end of the list.
- The length of the list will be increased by 1.
- It will update the original list itself.
- Return type is
None
.
num1=[1,2,3] num2=[4,5,6] num1.append(num2) print (num1) #Output:[1, 2, 3, [4, 5, 6]]
2. extend
- The extend method will extend the list by appending all the items from the iterable.
- The length of the list will be increased depends on the length of the iterable.
- It will update the original list itself.
- Return type is
None.
num1=[1,2,3] num2=[4,5,6] num1.extend(num2) print (num1) #Output:[1, 2, 3, 4, 5, 6]
3. Concatenation
- The list also supports concatenation operations.
- We can add two or more lists using
+
operator. - It won’t update the original list.
- Return type is a new
list object
.
Example 1:Concatenating two lists
num1=[1,2,3] num2=[4,5,6] num3=num1+num2 print (num3) #Output:[1, 2, 3, 4, 5, 6]
Example 2:Concatenating two or more lists
num1=[1,2,3] num2=[4,5,6] num3=[7,8] num4=[9,10] num5=num1+num2+num3+num4 print (num5) #Output:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
4. Unpacking
An asterisk *
denotes iterable unpacking. Its operand must be iterable. The iterable is expanded into a sequence of items, which are included in the new tuple, list, or set, at the site of the unpacking.list1=[*list2,*list3]
First, it will unpack the contents and then create a list from its contents.
num1=[1,2,3] num2=[4,5,6] num3=[*num1,*num2] print (num3) #Output:[1, 2, 3, 4, 5, 6]
5. itertools.chain
Makes an iterator that returns an element from the first iterable until its exhausted, then proceeds to the next iterable. It will treat consecutive sequences as a single sequence.itertools.chain(*iterables)
The list is also iterable, so we can use itertools.chain() to merge two dictionaries. The return type will be itertools.chain object. We can convert to a list using the list() constructor.
import itertools num1=itertools.chain([1,2,3],[4,5,6]) #Returns an iterator object print (num1)#Output:<itertools.chain object at 0x029FE4D8> #converting iterator object to list object print(list(num1))#Output:[1, 2, 3, 4, 5, 6]
6. List comprehension.
A list comprehension consists of brackets[]
containing an expression followed by a for
clause, then zero or more for
or if
clauses. The result will be a new list resulting from evaluating the expression in the context of the for
and if
clauses that follow it.[expression for item in iterable if conditional]
Example 1.Joining two lists using list comprehension
num1=[1,2,3] num2=[4,5,6] num3=[x for n in (num1,num2) for x in n] print (num3)#Output:[1, 2, 3, 4, 5, 6]
7. for loop
- First for loop to go through the lists. (for n in (num1,num2)
- Second for loop to go through the elements in the list. (for x in n)
- Then it will append each element to the empty list created before
num3
num1=[1,2,3] num2=[4,5,6] num3=[] for n in (num1,num2): for x in n: num3.append(x) print (num3) #Output:[1, 2, 3, 4, 5, 6]
How to join all strings in a list:str.join(iterable)
Return a string which is the concatenation of the strings in iterable. A
TypeError
will be raised if there are any non-string values in iterable, includingbytes
objects. The separator between elements is the string providing this method.
Example 1: Joining all strings in the list. The separator is given as space.
num1=["Welcome", "to", "python", "programming", "language"] num2=" ".join(num1) print (num2) #Output:Welcome to python programming language
Example 2: Joining all strings in the list. The separator is given as -
num1=["1","2","3"] num2="-".join(num1) print (num2) #Output:1-2-3
How to remove duplicate elements while merging two lists.
Python sets don’t contain duplicate elements.
To remove duplicate elements from lists we can convert the list to set using set() and then convert back to list using list() constructor.
num1=[1,2,3,4,5] num2=[1,3,5,7,9] num3=list(set(num1+num2)) print (num3) #Output:[1, 2, 3, 4, 5, 7, 9]
The fastest way to merge lists in python.
time.time() → float
Return the time in seconds since the epoch as a floating-point number. The epoch is the point where the time starts and is platform dependent. The specific date of the epoch and the handling of leap seconds is platform dependent.
Calculating the time taken by the append method
import time num1=list(range(1,1000000)) num2=list(range(1000000,2000000)) start=time.time() num1.append(num2) print (time.time()-start) #Output:0.0019817352294921875
Calculating the time taken to merge lists using the extend method.
import time num1=list(range(1,1000000)) num2=list(range(1000000,2000000)) start1=time.time() num1.extend(num2) print (time.time()-start1) #Output:0.00701141357421875
Calculating the time taken to join lists using concatenation operator.
import time num1=list(range(1,1000000)) num2=list(range(1000000,2000000)) start2=time.time() num1+num2 print (time.time()-start2) #Output:0.014112710952758789
Calculating the time taken to merge the lists using unpacking method
import time num1=list(range(1,1000000)) num2=list(range(1000000,2000000)) start3=time.time() [*num1,*num2] print (time.time()-start3) #Output:0.020873069763183594
Calculate the time taken to merge lists using itertools.chain()
import itertools import time num1=list(range(1,1000000)) num2=list(range(1000000,2000000)) start4=time.time() num3=itertools.chain(num1,num2) list(num3) print (time.time()-start4) #Output:0.045874595642089844
Calculate the time taken to merge lists using list comprehension.
import time num1=list(range(1,1000000)) num2=list(range(1000000,2000000)) start5=time.time() num3=[x for n in (num1,num2) for x in n] print (time.time()-start5) #Output:0.06680965423583984
Calculate the time taken to merge lists using for loop.
import time num1=list(range(1,1000000)) num2=list(range(1000000,2000000)) start5=time.time() num3=[] for n in (num1,num2): for x in n: num3.append(x) print (time.time()-start5) #Output:0.23975229263305664
Let’s compare the time taken by all methods to merge the lists.


Conclusion
- append method will add the list as one element to another list. The length of the list will be increased by one only after appending one list.
- The extend method will extend the list by appending all the items from iterable(another list). The length of the list will be increased depends on the length of the iterable.
- Both the append and extend method will modify the original list.
- Concatenation, unpacking, list comprehension returns a new list object. It won’t modify the original list
- itertools.chain()-Return type will be itertools.chain object. We can convert to a list using the list() constructor.
My other blog links
7 Different Ways to Merge Dictionaries in Python
List, Set, Dictionary Comprehensions in Python
Resources
Make a one-time donation
Make a monthly donation
Make a yearly donation
Choose an amount
Or enter a custom amount
Your contribution is appreciated.
Your contribution is appreciated.
Your contribution is appreciated.
Buy Me a CoffeeBuy Me a CoffeeBuy Me a Coffee