Python | Convert a list into tuple of lists
We are given a list, the task is to convert the list into tuple of lists.
Input: ['Geeks', 'For', 'geeks']
Output: (['Geeks'], ['For'], ['geeks'])
Input: ['first', 'second', 'third']
Output: (['first'], ['second'], ['third'])
Method #1: Using Comprehension
Python3
Input = [ 'Geeks' , 'for' , 'geeks' ]
Output = tuple ([name] for name in Input )
print (Output)
|
Output:
(['Geeks'], ['for'], ['geeks'])
Time Complexity: O(n), where n is the length of the Input list.
Auxiliary Space: O(n), as we are creating a new tuple of lists of the same length as Input.
Method #2 : Using Map + Lambda
Python3
Input = [ 'first' , 'second' , 'third' ]
Output = tuple ( map ( lambda x: [x], Input ))
print (Output)
|
Output:
(['first'], ['second'], ['third'])
Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(n), where n is the length of the input list.
Method #3 : Using Map + zip
Python3
Input = [ 'first' , 'second' , 'third' ]
Output = tuple ( map ( list , zip ( Input )))
print (Output)
|
Output:
(['first'], ['second'], ['third'])
Time complexity: O(n), where n is the length of the input list ‘Input’.
Auxiliary space: O(n), where n is the length of the input list ‘Input’.
Method #4: Using enumerate function
Python3
lst = [ 'Geeks' , 'for' , 'geeks' ]
x = tuple ([i] for a,i in enumerate (lst))
print (x)
|
Output
(['Geeks'], ['for'], ['geeks'])
Time Complexity: O(n), where n is the length of the list
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the list
Method#5: Using Recursive method.
Algorithm:
- Define a recursive function list_to_tuple_of_lists which takes a list lst as an input.
- If the list is empty, return an empty tuple.
- Otherwise, take the first element of the list and wrap it in a list.
- Wrap that list in a tuple, and concatenate the tuple with the result of recursively calling list_to_tuple_of_lists on the rest of the list.
- Return the resulting tuple.
Python3
def list_to_tuple_of_lists(lst):
if not lst:
return ()
else :
return ([lst[ 0 ]],) + list_to_tuple_of_lists(lst[ 1 :])
input_list = [ 'Geeks' , 'for' , 'geeks' ]
output_tuple = list_to_tuple_of_lists(input_list)
print (output_tuple)
|
Output
(['Geeks'], ['for'], ['geeks'])
Time Complexity:
The time complexity of the list_to_tuple_of_lists function is O(n), where n is the length of the input list. This is because the function makes a recursive call on a list of length n-1, and performs constant time operations (creating a list and wrapping it in a tuple) for each element of the list.
Auxiliary Space:
The space complexity of the list_to_tuple_of_lists function is also O(n), where n is the length of the input list. This is because the function creates a new list of length 1 for each element of the input list, and the maximum depth of the recursion is also n.
Method #6: Using a For Loop
Step-by-step approach:
- Initialize an empty list result to store the output.
- Use a for loop to iterate over each element of the input list.
- Append each element as a single-element list to the result list.
- Convert the result list to a tuple using the tuple() function.
- Assign the tuple to the output variable.
- Print the output variable.
Python3
input_list = [ 'Geeks' , 'for' , 'geeks' ]
result = []
for name in input_list:
result.append([name])
output = tuple (result)
print (output)
|
Output
(['Geeks'], ['for'], ['geeks'])
Time complexity: O(n), where n is the number of elements in the input list, because we iterate over each element of the list once.
Auxiliary space: O(n), where n is the number of elements in the input list, because we create a list of n lists to store the output before converting it to a tuple.
Last Updated :
20 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...