Skip to content
Related Articles

Related Articles

Python | Merging nested lists

Improve Article
Save Article
  • Last Updated : 18 Dec, 2021
Improve Article
Save Article

Given two nested lists, ‘lst1’ and ‘lst2’, write a Python program to create a new nested list ‘lst3’ out of the two given nested lists, so that the new nested list consist common intersections of both lists as well as the non-intersecting elements.

Examples: 

Input : lst1 = [[5, 9], [8, 2, 6], [3, 4]]
        lst2 = [[9, 5, 8], [2, 6], [3, 4, 1]]
Output : [[9, 5], [8], [2, 6], [3, 4], [1]]

Input : lst1 = [['a', 'b', 'c'], ['x']]
        lst2 = [['b', 'c', 'y'], ['x', 'y']]
Output : [['b', 'c'], ['x'], ['y'], ['a']]

Approach #1 : By set intersection using functools.reduce()
This method uses the set intersection to compute each intersection, then add any item which is leftover (that is, items appearing on only one of the two lists). We will first use functools.reduce() to yield unique elements of ‘lst1’ and ‘lst2’ and save them to ‘temp1’ and ‘temp2’ respectively. After this, find the set intersection of both the list in ‘lst3’. At last, find the symmetric difference of ‘lst1’ and ‘lst2’, which yields the items which appear in only one of the 2 sets and append it to ‘lst3’.

Python3




# Python3 program to find Greatest common
# intersection of two nested lists
import itertools
import functools
 
def GCI(lst1, lst2):
     
    temp1 = functools.reduce(lambda a, b: set(a).union(set(b)), lst1)
    temp2 = functools.reduce(lambda a, b: set(a).union(set(b)), lst2)
     
    lst3 = [list(set(a).intersection(set(b)))
           for a, b in itertools.product(lst1, lst2)
           if len(set(a).intersection(set(b))) != 0]
     
    lst3.extend([x] for x in temp1.symmetric_difference(temp2))
     
    return lst3
         
# Driver code
lst1 = [[5, 9], [8, 2, 6], [3, 4]]
lst2 = [[9, 5, 8], [2, 6], [3, 4, 1]]
print(GCI(lst1, lst2))

Output: 

[[9, 5], [8], [2, 6], [3, 4], [1]]

 

 
Approach #2 : By set intersection using filter
This is an efficient approach over approach #1 as it simplifies the intersection. First, flatten the nested lists. Take Intersection using filter() and save it to ‘lst3’. Now find elements either not in lst1 or in lst2, and save them to ‘temp’. Finally, append ‘temp’ to ‘lst3’. 

Python3




# Python3 program to find Greatest common
# intersection of two nested lists
import itertools
from functools import reduce
 
def GCI(lst1, lst2):
     
    temp1 = reduce(list.__add__, lst1)
    temp2 = reduce(list.__add__, lst2)
    lst3 = list(filter(None, [list(set(o1) & set(o2))
                     for o1 in lst1 for o2 in lst2])) 
 
    temp = filter(lambda x : x not in temp1 or x not in temp2,
                                      set(temp1) | set(temp2))
    lst3.append(list(temp))
     
    return lst3
         
# Driver code
lst1 = [[5, 9], [8, 2, 6], [3, 4]]
lst2 = [[9, 5, 8], [2, 6], [3, 4, 1]]
print(GCI(lst1, lst2))

Output: 

[[9, 5], [8], [2, 6], [3, 4], [1]]

 


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!