Open In App

Python | Convert a list into tuple of lists

Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python code to convert a list into tuple of lists
 
# Initialisation of list
Input = ['Geeks', 'for', 'geeks']
 
# Using list Comprehension
Output = tuple([name] for name in Input)
 
# printing output
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




# Python code to convert a list into tuple of lists
 
# Initialisation of list
Input = ['first', 'second', 'third']
 
# Using map + lambda
Output = tuple(map(lambda x: [x], Input))
 
# printing output
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




# Python code to convert a list into tuple of lists
 
# Initialisation of list
Input = ['first', 'second', 'third']
 
# Using Map + zip
Output = tuple(map(list, zip(Input)))
 
# printing output
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:

  1. Define a recursive function list_to_tuple_of_lists which takes a list lst as an input.
  2. If the list is empty, return an empty tuple.
  3. Otherwise, take the first element of the list and wrap it in a list.
  4. 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.
  5. 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:])
 
 
# example usage
input_list = ['Geeks', 'for', 'geeks']
output_tuple = list_to_tuple_of_lists(input_list)
print(output_tuple)  # output: (['Geeks'], ['for'], ['geeks'])


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




# Python code to convert a list into tuple of lists
 
# Initialization of list
input_list = ['Geeks', 'for', 'geeks']
 
# Using a for loop
result = []
for name in input_list:
    result.append([name])
 
# Convert result list to tuple
output = tuple(result)
 
# Print output
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
Previous
Next
Share your thoughts in the comments
Similar Reads