Join or Merge Lists In Python

In this notebook, we will go through the following topics

Join / Merge two lists in python using + operator.
Join / Merge two lists in python using list.extend().
Join / Merge two lists in python using unpacking.
Join / Merge two lists in python using itertools.
Join / Merge two lists in python using for loop.

Join / Merge two lists in Python using + operator

Let us create random list of numbers using Python random module.

In [1]:
import random
random.seed(4)
l1 = random.sample(range(0, 9), 5)
l2 = random.sample(range(0, 9), 5)
In [2]:
print(l1)
print(l2)
[3, 4, 0, 5, 8]
[7, 2, 0, 6, 5]

We can use + operator in Python to add two or more lists as long as the lists are of same type.

In [3]:
l1 + l2
Out[3]:
[3, 4, 0, 5, 8, 7, 2, 0, 6, 5]
In [4]:
print(l1)
print(l2)
[3, 4, 0, 5, 8]
[7, 2, 0, 6, 5]

We can use the '+' operator on list of characters or strings too.

In [5]:
l1 = 'john ludhi'.split(' ')
In [6]:
l1
Out[6]:
['john', 'ludhi']
In [7]:
l2 = 'elon musk'.split(' ')
In [8]:
l2
Out[8]:
['elon', 'musk']
In [9]:
l1 + l2
Out[9]:
['john', 'ludhi', 'elon', 'musk']

Join / Merge two lists in Python using list.extend()

In [10]:
import random
random.seed(4)
l1 = random.sample(range(0, 9), 5)
l2 = random.sample(range(0, 9), 5)
In [11]:
print(l1)
print(l2)
[3, 4, 0, 5, 8]
[7, 2, 0, 6, 5]
In [12]:
l1.extend(l2)

Let us print list l1

In [13]:
print(l1)
[3, 4, 0, 5, 8, 7, 2, 0, 6, 5]

We can use list.extend() method on list of strings or characters too.

In [14]:
l1 = 'john ludhi'.split(' ')
l2 = 'elon musk'.split(' ')
In [15]:
print(l1)
print(l2)
['john', 'ludhi']
['elon', 'musk']
In [16]:
l1.extend(l2)
In [17]:
print(l1)
['john', 'ludhi', 'elon', 'musk']

Join / Merge two lists in Python using unpacking

In [18]:
l1 = 'john ludhi'.split(' ')
l2 = 'elon musk'.split(' ')
In [19]:
mergelist = [*l1, *l2]
In [20]:
mergelist
Out[20]:
['john', 'ludhi', 'elon', 'musk']
In [21]:
import random
random.seed(4)
l1 = random.sample(range(0, 9), 5)
l2 = random.sample(range(0, 9), 5)
print(l1)
print(l2)
[3, 4, 0, 5, 8]
[7, 2, 0, 6, 5]
In [22]:
mergelist = [*l1, *l2]
print(mergelist)
[3, 4, 0, 5, 8, 7, 2, 0, 6, 5]

Join / Merge two lists in Python using itertools

We can use iterools module to chain two or more lists in Python

iterools.chain(list1,list2,...)

In [23]:
import random
random.seed(4)
l1 = random.sample(range(0, 9), 5)
l2 = random.sample(range(0, 9), 5)
In [24]:
import itertools
result = itertools.chain(l1,l2)
result
Out[24]:
<itertools.chain at 0x7f2c50787d30>

We can go through the above chain using for loop

In [25]:
import itertools
for l in itertools.chain(l1,l2):
    print(l)
3
4
0
5
8
7
2
0
6
5

Or we can convert in to a Python list.

In [26]:
list(itertools.chain(l1,l2))
Out[26]:
[3, 4, 0, 5, 8, 7, 2, 0, 6, 5]

We can chain multiple lists too with itertools.chain()

In [27]:
list(itertools.chain(l1,l2,l2))
Out[27]:
[3, 4, 0, 5, 8, 7, 2, 0, 6, 5, 7, 2, 0, 6, 5]

Join / Merge two lists in Python using for loop

In [28]:
import random
random.seed(4)
l1 = random.sample(range(0, 9), 5)
l2 = random.sample(range(0, 9), 5)
In [29]:
print(l1)
print(l2)
[3, 4, 0, 5, 8]
[7, 2, 0, 6, 5]
In [30]:
for elem in l2:
    l1.append(elem)
In [31]:
print(l1)
[3, 4, 0, 5, 8, 7, 2, 0, 6, 5]

To learn more about Python append, checkout following notebook
https://www.nbshare.io/notebook/767549040/Append-In-Python/