![]() |
VOOZH | about |
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 us 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 the above functions can be used to solve this problem. In this, we perform the conversion using tuple(), and list comprehension is used to extend the logic to all the containers.
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'))Time complexity: O(n*m), where n is the length of the input list and m is the length of the longest sublist.
Auxiliary Space: O(n*m), as the program creates a new tuple for each sublist in the input list, and each tuple contains the elements of the corresponding sublist. Therefore, the space used is proportional to the total number of elements in the input list.
Method #2 : Using map() + tuple()
The combination of the 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.
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'))Time complexity: O(n*m), where n is the number of sublists in the input list and m is the maximum length of a sublist.
Auxiliary space: O(n*m), since the tuple() function creates a new tuple for each sublist in the input list, and each tuple contains m elements.
Method #3: Using enumerate function
(('Gfg', 'is', 'Best'), ('Gfg', 'is', 'love'), ('Gfg', 'is', 'for', 'Geeks'))Time complexity: O(N*M), where N is the length of test_list and M is the maximum length of a sublist in test_list.
Auxiliary space: O(N*M)
Method#4: Using Recursive method
Algorithm:
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'))Time complexity:O(N), The function list_to_tuple is called recursively on a list that is one element smaller than the previous list, until the length of the list becomes 0. The number of times the function is called recursively is equal to the length of the input list. Therefore, the time complexity of the function is O(n), where n is the length of the input list.
Auxiliary space:O(N), The function list_to_tuple uses a constant amount of space to store the empty tuple (), which is returned when the input list is empty. In addition, the function creates a tuple for each element in the input list using the tuple function. Since each tuple created in the function is of the same length as the input list, the space complexity of the function is O(n), where n is the length of the input list.
Method #5: Using nested loops
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'))Time complexity: O(n^2) where n is the number of elements in the list of lists.
Auxiliary Space: O(n^2) because we are creating a new tuple for each sublist and then adding those tuples to a new list before creating a final tuple containing all the sub-tuples.
Method #6: Using itertools.starmap()
Approach:
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'))Time complexity: O(n), where n is the total number of elements in the input list of lists.
Auxiliary space: O(n), where n is the total number of elements in the input list of lists.
Method #7: Using a nested list comprehension and zip() function
This method involves using a nested list comprehension to iterate over the nested list and convert each inner list to a tuple. The zip function is then used to transpose the resulting list of tuples into a tuple of tuples.
Steps:
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'))Time complexity: O(n^2), where n is the length of the nested list.
Auxiliary space: O(n^2), for storing the tuple of tuples.