Sometimes, while working with Python data, we can have a problem in which we need to perform overlap of elements in Matrix, and convert them as tuple pairs. This kind of problem can occur in various application in data domain. Let’s discuss certain ways in which this task can be performed.
Input : test_list = [[5, 6, 3], [8, 6, 2], [2, 5, 1]]
Output : [(5, 6), (6, 3), (8, 6), (6, 2), (2, 5), (5, 1)]Input : test_list = [[5, 6, 3]]
Output : [(5, 6), (6, 3)]
Method #1 : Using loop
This is brute force way in which this task can be performed. In this, we iterate each list and extract consecutive elements and add them as pair in result list.
# Python3 code to demonstrate working of # Convert Matrix to overlapping Tuple Pairs # Using loop # initializing list test_list = [[5, 6, 7], [8, 6, 5], [2, 5, 7]] # printing original list print("The original list is : " + str(test_list)) # Convert Matrix to overlapping Tuple Pairs # Using loop res = [] for sub in test_list: res.append((sub[0], sub[1])) res.append((sub[1], sub[2])) # printing result print("Filtered tuples : " + str(res)) |
The original list is : [[5, 6, 7], [8, 6, 5], [2, 5, 7]] Filtered tuples : [(5, 6), (6, 7), (8, 6), (6, 5), (2, 5), (5, 7)]
Method #2 : Using loop + list slicing
The combination of above functionalities can be used to solve this problem. This offers flexibility to extend logic to more custom tuple sizes, more than 2 as well.
# Python3 code to demonstrate working of # Convert Matrix to overlapping Tuple Pairs # Using loop + list slicing # initializing list test_list = [[5, 6, 7], [8, 6, 5], [2, 5, 7]] # printing original list print("The original list is : " + str(test_list)) # Convert Matrix to overlapping Tuple Pairs # Using loop + list slicing res = [] for sub in test_list: for idx in range(len(sub) -1): res.append(tuple(sub[idx:idx + 2])) # printing result print("Filtered tuples : " + str(res)) |
The original list is : [[5, 6, 7], [8, 6, 5], [2, 5, 7]] Filtered tuples : [(5, 6), (6, 7), (8, 6), (6, 5), (2, 5), (5, 7)]
Recommended Posts:
- Python - Convert Tuple Matrix to Tuple List
- Python - Convert Tuple String to Integer Tuple
- Python - Convert Tuple to Tuple Pair
- Python - Convert Matrix to Custom Tuple Matrix
- Python | Sort tuple list by Nth element of tuple
- Python | Replace tuple according to Nth tuple element
- Python - Raise elements of tuple as power to another tuple
- Python - Flatten tuple of List to tuple
- Python | Find Maximum difference between tuple pairs
- Python | Maximum of Product Pairs in Tuple List
- Python - Pairs with Sum equal to K in tuple list
- Python - Similar index pairs in Tuple lists
- Python | Convert String to tuple list
- Python | Program to convert a tuple to a string
- Python | Convert a list into a tuple
- Python | Convert a list into tuple of lists
- Python | Convert list of tuple into dictionary
- Python | Convert list to indexed tuple list
- Python | Convert tuple records to single string
- Python | Convert location coordinates to tuple
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.

