Python | Merge two list of lists according to first element

Given two list of lists of equal length, write a Python program to merge the given two list of 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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output:

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

 
Method #2 : Python enumerate() with list comprehension

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output:

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

 
Method #3 : Python dictionary
In this method, we initialise ‘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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output:

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


My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.