Related Articles

# Python Program to Merge tuple list by overlapping mid tuple

• Last Updated : 29 Jun, 2021

Given two lists that contain tuples as elements, the task is to write a Python program to accommodate tuple from the second list between consecutive tuples from the first list, after considering ranges present between both the consecutive tuples from the first list.

Input : test_list1 = [(4, 8), (19, 22), (28, 30), (31, 50)], test_list2 = [(10, 12), (23, 26), (15, 20), (52, 58)]
Output : [((4, 8), (10, 12), (19, 22)), ((19, 22), (23, 26), (28, 30)), ((4, 8), (15, 20), (19, 22))]
Explanation : (4, 8) followed by (19, 22) can accommodate (10, 12) as 10 > 8 and 12 < 19.

Input : test_list1 = [(4, 8), (19, 22), (28, 30), (31, 50)], test_list2 = [(10, 22), (23, 26), (15, 20), (52, 58)]
Output : [((19, 22), (23, 26), (28, 30)), ((4, 8), (15, 20), (19, 22))]
Explanation : (23, 26) can be accommodated between tuples.

Method: Using loop

In this, we keep two pointers one for each container, and the other for each element in list 1. Now, check if any tuple from list 2 can satisfy the required condition, if not, the following consecutive elements are considered for the next set of iteration.

Example:

## Python3

 `# Python3 code to demonstrate working of``# Merge tuple list by overlapping mid tuple``# Using loop`` ` ` ` `# initializing lists``test_list1 ``=` `[(``4``, ``8``), (``19``, ``22``), (``28``, ``30``), (``91``, ``98``)]``test_list2 ``=` `[(``10``, ``22``), (``23``, ``26``), (``15``, ``20``), (``52``, ``58``)]`` ` `# printing original lists``print``(``"The original list 1 is : "` `+` `str``(test_list1))``print``(``"The original list 2 is : "` `+` `str``(test_list2))`` ` `idx ``=` `0``j ``=` `0``res ``=` `list``()`` ` `# iterating till anyone of list exhausts.``while` `j < ``len``(test_list2):`` ` `    ``# checking for mid tuple and appending``    ``if` `test_list2[j][``0``] > test_list1[idx][``0``]\``    ``and` `test_list2[j][``1``] < test_list1[idx ``+` `1``][``1``]:`` ` `        ``# appending the range element from 2nd list which ``        ``# fits consecution along with original elements ``        ``# from 1st list.``        ``res.append((test_list1[idx], test_list2[j], test_list1[idx ``+` `1``]))``        ``j ``+``=` `1``        ``idx ``=` `0``    ``else``:`` ` `        ``# if not, the 1st list is iterated and next two``        ``# ranges are compared for a fit.``        ``idx ``+``=` `1`` ` `    ``# restart indices once limits are checked.``    ``if` `idx ``=``=` `len``(test_list1) ``-` `1``:``        ``idx ``=` `0``        ``j ``+``=` `1`` ` `# printing result``print``(``"Merged Tuples : "` `+` `str``(res))`

Output:

The original list 1 is : [(4, 8), (19, 22), (28, 30), (91, 98)]

The original list 2 is : [(10, 22), (23, 26), (15, 20), (52, 58)]

Merged Tuples : [((19, 22), (23, 26), (28, 30)), ((4, 8), (15, 20), (19, 22)), ((28, 30), (52, 58), (91, 98))]

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up