Tuesday, November 19, 2024
Google search engine
HomeLanguagesPython | Merge overlapping part of lists

Python | Merge overlapping part of lists

Sometimes, while working with Python lists, we can have a problem in which we have to merge two lists’ overlapping parts. This kind of problem can come in day-day programming domain. Let’s discuss a way in which this problem can be solved. 

Method 1: 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 the first list till the match of the initial slice of the second list. Then, we get the first such overlapping( max overlap ) using next(), and then we join the remaining part using list slicing. 

Python3




# Python3 code to demonstrate working of
# Merge overlapping parts of list
# using generator + next() + list slicing
 
# initialize lists
test_list1 = [4, 5, 7, 9, 10, 11]
test_list2 = [10, 11, 16, 17]
 
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
# Merge overlapping parts of list
# using generator + next() + list slicing
temp = (i for i in range(len(test_list2), 0, -1) if test_list2[:i] == test_list1[-i:])
temp2 = next(temp, 0)
res = test_list1 + test_list2[temp2 : ]
 
# printing result
print("List after overlapping merge is : " + str(res))


Output : 

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]

Time complexity: O(n), where n is the sum of the lengths of the input lists.
Auxiliary space: O(1),

Method 2: Using iteration

We can approach this problem by iterating through both lists and checking for overlapping parts. We can start by comparing the first element of each list and adding the smaller one to the merged list. Then, we continue comparing the next elements of both lists and adding the smaller ones until we find an element that is present in both lists. At this point, we add all the elements in the overlapping part to the merged list and continue comparing the remaining elements. Finally, we add any remaining elements from either list to the merged list.

Steps:

  1. Initialize an empty list called merged_list and also Initialize two pointers i and j to point to the first element of list1 and list2, respectively.
  2. Compare the ith element of list1 and jth element of list2:
    • If the ith element of list1 is less than the jth element of list2, add the ith element to the merged list and increment i by 1.
    • If the jth element of list2 is less than the ith element of list1, add the jth element to the merged list and increment j by 1.
    • If the ith element of list1 is equal to the jth element of list2, add all the elements in the overlapping part to the merged list, increment i and j by 1, and reset the overlapping part to an empty list.
  3. Repeat step 3 until one of the pointers reaches the end of its list.
  4. Add any remaining elements from either list to the merged list and return the merged list. 

Python3




def merge_lists(list1, list2):
 
    # Enpty list
    merged_list = []
 
    i, j = 0, 0
    overlapping_part = []
 
    while i < len(list1) and j < len(list2):
 
        # When element of list2 > list1
        if list1[i] < list2[j]:
            merged_list.append(list1[i])
            i += 1
 
        # When element of list1 > list2
        elif list2[j] < list1[i]:
            merged_list.append(list2[j])
            j += 1
        else:
            overlapping_part.append(list1[i])
            i += 1
            j += 1
 
            while i < len(list1) and j < len(list2) and list1[i] == list2[j]:
                overlapping_part.append(list1[i])
                i += 1
                j += 1
 
            merged_list.extend(overlapping_part)
            overlapping_part = []
    merged_list.extend(list1[i:])
    merged_list.extend(list2[j:])
    return merged_list
 
 
# Initializing lists
list1 = [4, 5, 7, 9, 10, 11]
list2 = [10, 11, 16, 17]
 
# Printing answer
print(merge_lists(list1, list2))


Output

[4, 5, 7, 9, 10, 11, 16, 17]

Time Complexity: O(n), where n is the length of the merged list.
Auxiliary Space: O(n), where n is the length of the merged list.

Method 3: using the concept of pointers.

 Stepwise Approach: 

  1. Initialize two pointers: ptr1 and ptr2 to the start of test_list1 and test_list2 respectively.
  2. Create an empty list merged_list to store the merged elements.
  3. While both pointers are within the bounds of their respective lists, compare the elements at ptr1 and ptr2.
  4. If test_list1[ptr1] is less than test_list2[ptr2], append test_list1[ptr1] to merged_list and increment ptr1.
  5. If test_list1[ptr1] is greater than test_list2[ptr2], append test_list2[ptr2] to merged_list and increment ptr2.
  6. If test_list1[ptr1] is equal to test_list2[ptr2], append either test_list1[ptr1] or test_list2[ptr2] to merged_list and increment both pointers.
  7. Once one of the pointers has gone out of bounds, append the remaining elements from the other list to merged_list.
  8. Return merged_list.

Python3




# Python3 code to demonstrate working of
# Merge overlapping parts of list
# using pointers
 
# initialize lists
test_list1 = [4, 5, 7, 9, 10, 11]
test_list2 = [10, 11, 16, 17]
 
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
# Merge overlapping parts of list
# using pointers
ptr1, ptr2 = 0, 0
merged_list = []
 
while ptr1 < len(test_list1) and ptr2 < len(test_list2):
    if test_list1[ptr1] < test_list2[ptr2]:
        merged_list.append(test_list1[ptr1])
        ptr1 += 1
    elif test_list1[ptr1] > test_list2[ptr2]:
        merged_list.append(test_list2[ptr2])
        ptr2 += 1
    else:
        merged_list.append(test_list1[ptr1])
        ptr1 += 1
        ptr2 += 1
 
# append remaining elements
if ptr1 < len(test_list1):
    merged_list.extend(test_list1[ptr1:])
if ptr2 < len(test_list2):
    merged_list.extend(test_list2[ptr2:])
 
# printing result
print("List after overlapping merge is : " + str(merged_list))


Output

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]

The time complexity of this approach is O(n), where n is the total number of elements in the two lists.

The auxiliary space used is O(n), where n is the length of the merged list.

Method 4: Using Set Intersection

  1. Create two sets from the given lists – set1 and set2.
  2. Take the intersection of the sets using the & operator to get the overlapping elements.
  3. Create a new list merged_list by iterating through both the input lists and adding elements to it in the following order:
  4. Elements from test_list1 that are not in the intersection.
  5. Elements from the intersection in the order they appear in test_list1.
  6. Elements from test_list2 that are not in the intersection.
  7. Return the merged_list.

Python3




test_list1 = [4, 5, 7, 9, 10, 11]
test_list2 = [10, 11, 16, 17]
 
# Step 1
set1 = set(test_list1)
set2 = set(test_list2)
 
# Step 2
intersection = set1 & set2
 
# Step 3: Empty list
merged_list = []
for num in test_list1:
    if num not in intersection:
        merged_list.append(num)
for num in test_list2:
    if num not in intersection:
        merged_list.append(num)
for num in test_list1:
    if num in intersection:
        merged_list.append(num)
 
# Step 4: Printing result
print("List after overlapping merge is : " + str(merged_list))


Output

List after overlapping merge is : [4, 5, 7, 9, 16, 17, 10, 11]

Time Complexity: O(n+m), where n and m are the lengths of the input lists.
Auxiliary Space: O(n+m), where n and m are the lengths of the input lists.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments