Python | Merging nested lists
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 left over (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 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)) |
[[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 intetsection. 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 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)) |
[[9, 5], [8], [2, 6], [3, 4], [1]]
Recommended Posts:
- Python | Split nested list into two lists
- Python | Check if a nested list is a subset of another nested list
- Python | Merging two Dictionaries
- Python | Merging two list of dictionaries
- Python | Merging two strings with Suffix and Prefix
- Python | Program to count number of lists in a list of lists
- Python | Nested Dictionary
- Python | Zipping two lists of lists
- Python | Sum values for each key in nested dictionary
- Python | How to copy a nested list
- Python | Sort nested dictionary by key
- Python | Unpacking nested tuples
- Addition in Nested Tuples - Python
- Python | Add keys to nested dictionary
- Nested List Comprehensions in Python
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.

