Python – Convert List of Lists to Tuple of Tuples

Sometimes, while working with Python data, we can have a problem in which we need to perform interconversion of data types. This kind of problem can occur in domains in which we need to get data in particular formats such as Machine Learning. Let’s discuss certain ways in which this task can be performed.

Input : test_list = [[‘Best’], [‘Gfg’], [‘Gfg’]]
Output : ((‘Best’, ), (‘Gfg’, ), (‘Gfg’, ))

Input : test_list = [[‘Gfg’, ‘is’, ‘Best’]]
Output : ((‘Gfg’, ‘is’, ‘Best’), )

Method #1 : Using tuple() + list comprehension
The combination of above functions can be used to solve this problem. In this, we perform the conversion using tuple() and list comprehension is used to extend logic to all the containers.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Convert List of Lists to Tuple of Tuples
# Using tuple + list comprehension
  
# initializing list
test_list = [['Gfg', 'is', 'Best'], ['Gfg', 'is', 'love'],
                            ['Gfg', 'is', 'for', 'Geeks']]
  
# printing original list
print("The original list is : " + str(test_list))
  
# Convert List of Lists to Tuple of Tuples
# Using tuple + list comprehension
res = tuple(tuple(sub) for sub in test_list)
  
# printing result 
print("The converted data : " + str(res)) 

chevron_right


Output :



The original list is : [[‘Gfg’, ‘is’, ‘Best’], [‘Gfg’, ‘is’, ‘love’], [‘Gfg’, ‘is’, ‘for’, ‘Geeks’]]
The converted data : ((‘Gfg’, ‘is’, ‘Best’), (‘Gfg’, ‘is’, ‘love’), (‘Gfg’, ‘is’, ‘for’, ‘Geeks’))

 

Method #2 : Using map() + tuple()
The combination of above functions can be used to solve this problem. In this, we perform the task performed using list comprehension using map(), to extend the conversion logic to each sublist.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Convert List of Lists to Tuple of Tuples
# Using map() + tuple()
  
# initializing list
test_list = [['Gfg', 'is', 'Best'], ['Gfg', 'is', 'love'],
                             ['Gfg', 'is', 'for', 'Geeks']]
  
# printing original list
print("The original list is : " + str(test_list))
  
# Convert List of Lists to Tuple of Tuples
# Using map() + tuple()
res = tuple(map(tuple, test_list))
  
# printing result 
print("The converted data : " + str(res)) 

chevron_right


Output :

The original list is : [[‘Gfg’, ‘is’, ‘Best’], [‘Gfg’, ‘is’, ‘love’], [‘Gfg’, ‘is’, ‘for’, ‘Geeks’]]
The converted data : ((‘Gfg’, ‘is’, ‘Best’), (‘Gfg’, ‘is’, ‘love’), (‘Gfg’, ‘is’, ‘for’, ‘Geeks’))




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.