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]
- Python | Merge key value lists
- Python | Merge two lists alternatively
- Python | Merge corresponding sublists from two different lists
- Python | Merge two list of lists according to first element
- Python | Merge two lists into list of tuples
- Python | Merge List with common elements in a List of Lists
- Python | Program to count number of lists in a list of lists
- Python | Count overlapping substring in a given string
- Python | Zipping two lists of lists
- Python | Part of Speech Tagging using TextBlob
- Python | Merge Python key values to list
- Python | Sort given list of strings by part of string
- Python | Check if substring is part of List of Strings
- Part of Speech Tagging with Stop words using NLTK in python
- Python PIL | Image.merge() method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.