Open In App

Python – Split list into all possible tuple pairs

Last Updated : 23 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a list, the task is to write a python program that can split it into all possible tuple pairs combinations. 

Input : test_list = [4, 7, 5, 1, 9]

Output : [[4, 7, 5, 1, 9], [4, 7, 5, (1, 9)], [4, 7, (5, 1), 9], [4, 7, (5, 9), 1], [4, (7, 5), 1, 9], [4, (7, 5), (1, 9)], [4, (7, 1), 5, 9], [4, (7, 1), (5, 9)], [4, (7, 9), 5, 1], [4, (7, 9), (5, 1)], [(4, 7), 5, 1, 9], [(4, 7), 5, (1, 9)], [(4, 7), (5, 1), 9], [(4, 7), (5, 9), 1], [(4, 5), 7, 1, 9], [(4, 5), 7, (1, 9)], [(4, 5), (7, 1), 9], [(4, 5), (7, 9), 1], [(4, 1), 7, 5, 9], [(4, 1), 7, (5, 9)], [(4, 1), (7, 5), 9], [(4, 1), (7, 9), 5], [(4, 9), 7, 5, 1], [(4, 9), 7, (5, 1)], [(4, 9), (7, 5), 1], [(4, 9), (7, 1), 5]]

Explanation : All pairs partitions are formed.

Input : test_list = [4, 7, 5, 1]

Output : [[4, 7, 5, 1], [4, 7, (5, 1)], [4, (7, 5), 1], [4, (7, 1), 5], [(4, 7), 5, 1], [(4, 7), (5, 1)], [(4, 5), 7, 1], [(4, 5), (7, 1)], [(4, 1), 7, 5], [(4, 1), (7, 5)]]

Explanation : All pairs partitions are formed.

Approach: Using slicing and recursion

In this, we perform all pair creation from 1st element, and using recursion multiple elements are converted into pairs by appropriate partitioning.

Python3




def pairings(test_list):
    if len(test_list) <= 1:
        return [test_list]
 
    # keeping 1st element and attaching every element with it
    parts = [[test_list[0]] + ele for ele in pairings(test_list[1:])]
    for idx in range(1, len(test_list)):
 
        # pairing all possible from second element
        parts.extend([[(test_list[0], test_list[idx])] +
                      ele for ele in pairings(test_list[1: idx]
                                              + test_list[idx + 1:])])
 
    return parts
 
 
# initializing list
test_list = [4, 7, 5, 1]
 
# printing original list
print("The original list is : " + str(test_list))
 
# calling util. function
res = pairings(test_list)
 
# printing result
print("Created partitions : " + str(res))


Output:

The original list is : [4, 7, 5, 1]

Created partitions : [[4, 7, 5, 1], [4, 7, (5, 1)], [4, (7, 5), 1], [4, (7, 1), 5], [(4, 7), 5, 1], [(4, 7), (5, 1)], [(4, 5), 7, 1], [(4, 5), (7, 1)], [(4, 1), 7, 5], [(4, 1), (7, 5)]]

Time Complexity: O(n)
Auxiliary Space: O(n)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads