Transpose Dual Tuple List in Python

Sometimes, while working with Python tuples, we can have a problem in which we need to perform tuple transpose of elements i.e, each column element of dual tuple becomes a row, a 2*N Tuple becomes N * 2 Tuple List. This kind of problem can have possible applications in domains such as web development and day-day programming. Let’s discuss certain ways in which this task can be performed.

Input : test_list = [(2, 'Gfg'), (3, 'is'), (94, 'Best')]
Output : ([2, 3, 94], ['Gfg', 'is', 'Best'])
Input : test_list = [(8, 1)]
Output : ([8, 1])

Method #1: Using loop

This is one of the ways in which this problem can be solved. In this, we employ a brute force strategy to perform the transpose by constructing 2 lists and combining them to get the transposed results.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Transpose Tuple List
# Using loop
  
# hlper_fnc function
def hlper_fnc(test_list):
    # declare empty list
    sub1 = []
    sub2 = []
  
    res = (sub1, sub2)
    for sub in test_list:
        # add element in the last of the list
        sub1.append(sub[0])
        sub2.append(sub[1])
  
    return res
  
# initializing list
test_list = [(5, 1), (3, 4), (9, 7), (10, 6)]
  
# printing original list
print("The original list is : " + str(test_list))
  
# Transpose Tuple List
# Using loop
res = hlper_fnc(test_list)
  
# printing result 
print("The transposed tuple list : " + str(res)) 

chevron_right


Output:

The original list is : [(5, 1), (3, 4), (9, 7), (10, 6)]
The transposed tuple list : ([5, 3, 9, 10], [1, 4, 7, 6])

Method #2 : Using loop + zip() + tuple()

The combination of above functionalities can also be used to solve this problem. In this, we perform the task of forming transpose and extract columner elements using zip().

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Transpose Tuple List
# Using zip() + tuple()
  
# hlper_fnc function
def hlper_fnc(test_list):
    return tuple(zip(*test_list))
  
# initializing list
test_list = [(5, 1), (3, 4), (9, 7), (10, 6)]
  
# printing original list
print("The original list is : " + str(test_list))
  
# Transpose Tuple List
# Using zip() + tuple()
sub1, sub2 = hlper_fnc(test_list) 
res = (list(sub1), list(sub2))
  
# printing result 
print("The transposed tuple list : " + str(res)) 

chevron_right


Output:

The original list is : [(5, 1), (3, 4), (9, 7), (10, 6)]
The transposed tuple list : ([5, 3, 9, 10], [1, 4, 7, 6])



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.