Skip to content
Related Articles

Related Articles

Python – Convert Tuple Matrix to Tuple List

Improve Article
Save Article
  • Last Updated : 08 Aug, 2022
Improve Article
Save Article

Given a Tuple Matrix, flatten to tuple list with each tuple representing each column.

Input : test_list = [[(4, 5), (7, 8)], [(10, 13), (18, 17)]] 

Output : [(4, 7, 10, 18), (5, 8, 13, 17)] 

Explanation : All column number elements contained together. 

Input : test_list = [[(4, 5)], [(10, 13)]] 

Output : [(4, 10), (5, 13)] 

Explanation : All column number elements contained together.

Method #1 : Using list comprehension + zip()

In this, we perform task of flattening using list comprehension and zip() is used to perform column pairing to render as tuple pairs. 

Python3




# Python3 code to demonstrate working of
# Convert Tuple Matrix to Tuple List
# Using list comprehension + zip()
 
# initializing list
test_list = [[(4, 5), (7, 8)], [(10, 13), (18, 17)], [(0, 4), (10, 1)]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# flattening
temp = [ele for sub in test_list for ele in sub]
 
# joining to form column pairs
res = list(zip(*temp))
 
# printing result
print("The converted tuple list : " + str(res))


Output

The original list is : [[(4, 5), (7, 8)], [(10, 13), (18, 17)], [(0, 4), (10, 1)]]
The converted tuple list : [(4, 7, 10, 18, 0, 10), (5, 8, 13, 17, 4, 1)]

Method #2 : Using chain.from_iterable() + zip()

In this, task of flattening is performed using chain.from_iterable() and zip() is used to perform the task of column pairing.

Python3




# Python3 code to demonstrate working of
# Convert Tuple Matrix to Tuple List
# Using chain.from_iterable() + zip()
from itertools import chain
 
# initializing list
test_list = [[(4, 5), (7, 8)], [(10, 13), (18, 17)], [(0, 4), (10, 1)]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# flattening using from_iterable
res = list(zip(*chain.from_iterable(test_list)))
 
# printing result
print("The converted tuple list : " + str(res))


Output

The original list is : [[(4, 5), (7, 8)], [(10, 13), (18, 17)], [(0, 4), (10, 1)]]
The converted tuple list : [(4, 7, 10, 18, 0, 10), (5, 8, 13, 17, 4, 1)]

Method #3: Using extend(),list() and tuple() methods

Python3




# Python3 code to demonstrate working of
# Convert Tuple Matrix to Tuple List
 
# initializing list
test_list = [[(4, 5), (7, 8)], [(10, 13), (18, 17)], [(0, 4), (10, 1)]]
 
# printing original list
print("The original list is : " + str(test_list))
 
x=[]
for i in test_list:
    for j in i:
        j=list(j)
        x.extend(j)
re1=[]
re2=[]
for i in range(0,len(x)):
    if(i%2==0):
        re1.append(x[i])
    else:
        re2.append(x[i])
res=[]
res.append(tuple(re1))
res.append(tuple(re2))
 
# printing result
print("The converted tuple list : " + str(res))


Output

The original list is : [[(4, 5), (7, 8)], [(10, 13), (18, 17)], [(0, 4), (10, 1)]]
The converted tuple list : [(4, 7, 10, 18, 0, 10), (5, 8, 13, 17, 4, 1)]

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!