Skip to content
Related Articles

Related Articles

Python | Merge two list of lists according to first element

View Discussion
Improve Article
Save Article
  • Last Updated : 06 Sep, 2022
View Discussion
Improve Article
Save Article

Given two list of lists of equal length, write a Python program to merge the given two lists, according to the first common element of each sublist. 

Examples:

Input : lst1 = [[1, 'Alice'], [2, 'Bob'], [3, 'Cara']]
        lst2 = [[1, 'Delhi'], [2, 'Mumbai'], [3, 'Chennai']]
Output : [[1, 'Alice', 'Delhi'], [2, 'Bob', 'Mumbai'], [3, 'Cara', 'Chennai']]
Input : lst1 = [ ['c', 'class'], ['g', 'greek'], ]
        lst2 = [['c', 'coder'], ['g', 'god'], ]
Output : [['c', 'class', 'coder'], ['g', 'greek', 'god']]

Method #1: Python zip() with list comprehension 

Python3




# Python3 program to Merge two list of
# lists according to first element
 
 
def merge(lst1, lst2):
    return [a + [b[1]] for (a, b) in zip(lst1, lst2)]
 
# Driver code
lst1 = [[1, 'Alice'], [2, 'Bob'], [3, 'Cara']]
lst2 = [[1, 'Delhi'], [2, 'Mumbai'], [3, 'Chennai']]
print(merge(lst1, lst2))

Output:

[[1, 'Alice', 'Delhi'], [2, 'Bob', 'Mumbai'], [3, 'Cara', 'Chennai']]

  Method #2 : Python enumerate() with list comprehension 

Python3




# Python3 program to Merge two list of
# lists according to first element
 
import collections
 
def merge(lst1, lst2):
    return [(sub + [lst2[i][-1]]) for i, sub in enumerate(lst1)]
     
# Driver code
lst1 = [[1, 'Alice'], [2, 'Bob'], [3, 'Cara']]
lst2 = [[1, 'Delhi'], [2, 'Mumbai'], [3, 'Chennai']]
print(merge(lst1, lst2))

Output:

[[1, 'Alice', 'Delhi'], [2, 'Bob', 'Mumbai'], [3, 'Cara', 'Chennai']]

Method #3: Python dictionary In this method, we initialize ‘dict1’ with collections.defaultdict and traverse through ‘lst1’+’lst2’ and append the first element of ‘lst1’ as key and tupled second element of both respective sublists as value. Finally, we traverse through ‘dict1’ and initialize ‘dictlist’ with the desired output. 

Python3




# Python3 program to Merge two list of
# lists according to first element
import collections
 
 
def merge(lst1, lst2):
    dict1 = collections.defaultdict(list)
 
    for e in lst1 + lst2:
        dict1[e[0]].append(e[1])
    dictlist = list()
 
    for key, value in dict1.items():
        dictlist.append([key]+value)
 
    return dictlist
 
 
# Driver code
lst1 = [[1, 'Alice'], [2, 'Bob'], [3, 'Cara']]
lst2 = [[1, 'Delhi'], [2, 'Mumbai'], [3, 'Chennai']]
print(merge(lst1, lst2))

Output:

[[1, 'Alice', 'Delhi'], [2, 'Bob', 'Mumbai'], [3, 'Cara', 'Chennai']]

Method #4: Using extend() and for loop

Python3




# Python3 program to Merge two list of
# lists according to first element
 
lst1 = [[1, 'Alice'], [2, 'Bob'], [3, 'Cara']]
lst2 = [[1, 'Delhi'], [2, 'Mumbai'], [3, 'Chennai']]
lst1.extend(lst2)
x=[]
res=[]
for i in lst1:
    if i[0] not in x:
        x.append(i[0])
for i in x:
    p=[]
    p.append(i)
    for j in lst1:
        if(j[0]==i):
            p.append(j[1])
    res.append(p)
     
print(res)

Output

[[1, 'Alice', 'Delhi'], [2, 'Bob', 'Mumbai'], [3, 'Cara', 'Chennai']]

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!