Sometimes, while working with Python tuples, we can have a problem in which we need to perform similar index pairing. This kind of problem is peculiar, but can occur across certain domains. Let’s discuss certain way in which this task can be performed.
test_list1 = [(5, ), (1, ), (8, ), (10, )]
test_list2 = [(8, ), (1, ), (11, ), (9, )]
Output : [[(5, 8)], [(1, 1)], [(8, 11)], [(10, 9)]]
test_list1 = [(5, 6, 7, 6)]
test_list2 = [(8, 6, 7, 9)]
Output : [[(5, 8), (6, 6), (7, 7), (6, 9)]]
Method : Using list comprehension +
The combination of above functions can be used to solve this problem. In this, we perform the task of zipping similar index elements using zip() and list comprehension is used to compile all the pairs.
The original list 1 is : [(5, 6), (1, 2), (8, 9), (10, 33)] The original list 2 is : [(8, 7), (1, 3), (11, 23), (9, 4)] The paired tuples : [[(5, 8), (6, 7)], [(1, 1), (2, 3)], [(8, 11), (9, 23)], [(10, 9), (33, 4)]]
- Python - Convert Lists into Similar key value lists
- Python - Elements with K lists similar index value
- Python | Remove similar element rows in tuple Matrix
- Python | Record Similar tuple occurrences
- Python - Test Similar Data Type in Tuple
- Python - Remove Similar Rows from 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 - Convert Tuple String to Integer Tuple
- Python - Convert Tuple to Tuple Pair
- Python - Flatten tuple of List to tuple
- Python - Convert Tuple Matrix to Tuple List
- Python | Sorting list of lists with similar list elements
- Python | Extract similar index elements
- Python | Kth index character similar Strings
- Python - Similar index elements frequency
- Python - Similar index elements Matrix
- Python - Filter index similar values
- Python - Group Records on Similar index elements
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.