Python | Merge overlapping part of lists
Sometimes, while working with Python lists, we can have a problem in which we have to merge two list’s overlapping part. This kind of problem can come in day-day programming domain. Let’s discuss a way in which this problem can be solved.
Method : Using generator +
next() + list slicing
This method can be employed to solve this task. In this, first, we iterate a variable from rear end of one of list, and get the slice of rear end of first list till match of initial slice of second list. Then, we get the first such overlapping( max overlap ) using
next() and then we join the remaining part using list slicing.
The original list 1 is : [4, 5, 7, 9, 10, 11] The original list 2 is : [10, 11, 16, 17] List after overlapping merge is : [4, 5, 7, 9, 10, 11, 16, 17]