# Python – Convert List to Single valued Lists in Tuple

Conversion of data types is the most common problem across CS domain nowdays. One such problem can be converting List elements to single values lists in tuples. This can have application in data preprocessing domain. Let’s discuss certain ways in which this task can be performed.

Input : test_list = [1, 3, 5, 6, 7, 9]
Output : ([1], [3], [5], [6], [7], [9])

Input : test_list = [1]
Output : ([1])

Method #1: Using list comprehension + tuple() This is one of the ways in which this task can be performed. In this, we construct and iterate the list using list comprehension and finally convert the result list to tuple using tuple().

## Python3

 `# Python3 code to demonstrate working of ``# Convert List to Single valued Lists in Tuple``# Using list comprehension + tuple()` `# initializing lists``test_list ``=` `[``6``, ``8``, ``4``, ``9``, ``10``, ``2``]` `# printing original list``print``("The original ``list` `is` `: " ``+` `str``(test_list))` `# Convert List to Single valued Lists in Tuple``# Using list comprehension + tuple()``res ``=` `tuple``([ele] ``for` `ele ``in` `test_list)` `# printing result ``print``("``Tuple` `after conversion : " ``+` `str``(res)) `

Output :
```The original list is : [6, 8, 4, 9, 10, 2]
Tuple after conversion : ([6], [8], [4], [9], [10], [2])```

Time complexity: O(n), where n is the length of the input list test_list.
Auxiliary space: O(n), as we are creating a new list with single-valued lists for each element in the input list, which has the same length as the input list. This new list is stored in the variable res.

Method #2 : Using map() + list() + zip() + tuple() The combination of above functions can be used to solve this problem. In this, we perform task of extending logic of conversion to each element using map(), and list conversion to each element using zip(). At end, the result is converted back to tuple using tuple().

## Python3

 `# Python3 code to demonstrate working of ``# Convert List to Single valued Lists in Tuple``# Using map() + list() + zip() + tuple()` `# initializing lists``test_list ``=` `[``6``, ``8``, ``4``, ``9``, ``10``, ``2``]` `# printing original list``print``("The original ``list` `is` `: " ``+` `str``(test_list))` `# Convert List to Single valued Lists in Tuple``# Using map() + list() + zip() + tuple()``res ``=` `tuple``(``map``(``list``, ``zip``(test_list)))` `# printing result ``print``("``Tuple` `after conversion : " ``+` `str``(res)) `

Output :
```The original list is : [6, 8, 4, 9, 10, 2]
Tuple after conversion : ([6], [8], [4], [9], [10], [2])```

Time complexity: O(N)
Auxiliary space: O(N)

#### Method #2 : Using map() + lambda

In this approach, the map() function is used to apply a lambda function to each element of the original list. The lambda function creates a single-valued list containing the element as its only value. The resulting map object is then converted to a tuple using the tuple() function.

## Python3

 `# Define an original list of numbers``original_list ``=` `[``6``, ``8``, ``4``, ``9``, ``10``, ``2``]` `# Convert the original list into a tuple of lists using the map function``# The lambda function is used to create a new list containing a single element of the original list``# The resulting tuple will contain multiple lists, with each list containing one element from the original list``tuple_of_lists ``=` `tuple``(``map``(``lambda` `x: [x], original_list))` `# Print the original list and the tuple of lists``print``(``"The original list is :"``, original_list)``print``(``"Tuple after conversion :"``, tuple_of_lists)`

Output
```The original list is : [6, 8, 4, 9, 10, 2]
Tuple after conversion : ([6], [8], [4], [9], [10], [2])```

Time complexity: O(n)
Auxiliary space: O(n)

Method#4: Using Recursive method.

Algorithm:

1. Define a recursive function list_to_tuple_of_single_valued_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 put it in a new list.
4. Wrap that list in a tuple, and concatenate the tuple with the result of recursively calling list_to_tuple_of_single_valued_lists on the rest of the list.
5. Return the resulting tuple.

## Python3

 `def` `list_to_tuple_of_single_valued_lists(lst):``    ``if` `not` `lst:``        ``return` `()``    ``else``:``        ``return` `([lst[``0``]],) ``+` `list_to_tuple_of_single_valued_lists(lst[``1``:])` `input_list ``=` `[``6``, ``8``, ``4``, ``9``, ``10``, ``2``]``output_tuple ``=` `list_to_tuple_of_single_valued_lists(input_list)``# Print the original list and the tuple of lists``print``(``"The original list is :"``, input_list)``print``(``"Tuple after conversion :"``, output_tuple)`

Output
```The original list is : [6, 8, 4, 9, 10, 2]
Tuple after conversion : ([6], [8], [4], [9], [10], [2])```

Time Complexity:
The time complexity of the list_to_tuple_of_single_valued_lists function is O(n^2), where n is the length of the input list. This is because the function concatenates a tuple of length 1 with the result of recursively calling itself on a list of length n-1, resulting in a tuple of length n. Each concatenation takes O(n) time, so the total time complexity is O(n^2).

Auxiliary Space:
The space complexity of the list_to_tuple_of_single_valued_lists function is also O(n^2), where n is the length of the input list. This is because the function creates a new tuple of length 1 for each element of the input list, and the maximum depth of the recursion is also n. Therefore, the space complexity is proportional to the square of the length of the input list.

Method #5: Using a for loop and append()

This approach involves using a for loop to iterate through the elements of the input list and then using the append() method to create a new list containing single-valued lists. Finally, we can use the tuple() method to convert the list into a tuple.

Step-by-step approach:

• Initialize an empty list res to store the single-valued lists.
• Use a for loop to iterate through the elements of the input list test_list.
• For each element ele in test_list, create a new list [ele] containing the single value.
• Append the new list to res.
• Use the tuple() method to convert the list res into a tuple res_tuple.
• Return the tuple res_tuple.

## Python3

 `# Python3 code to demonstrate working of ``# Convert List to Single valued Lists in Tuple``# Using a for loop and append()` `# initializing list``test_list ``=` `[``6``, ``8``, ``4``, ``9``, ``10``, ``2``]` `# printing original list``print``(``"The original list is: "` `+` `str``(test_list))` `# Convert List to Single valued Lists in Tuple``# Using a for loop and append()``res ``=` `[]``for` `ele ``in` `test_list:``    ``res.append([ele])``res_tuple ``=` `tuple``(res)` `# printing result ``print``(``"Tuple after conversion: "` `+` `str``(res_tuple)) `

Output
```The original list is: [6, 8, 4, 9, 10, 2]
Tuple after conversion: ([6], [8], [4], [9], [10], [2])```

Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(n), to store the output list.

Method #6: Using list() + tuple() + generator expression

• Create a list comprehension that creates a list of lists where each inner list contains a single integer from the original list. The outer list is created using the list() constructor, which converts the list comprehension into a list. The inner lists are created using a generator expression that iterates over the original list and creates a new list containing a single integer.
• Convert the list of lists to a tuple using the tuple() constructor. This creates a tuple of tuples where each inner tuple contains a single integer from the original list.
• Assign the resulting tuple to a new variable called res_tuple.
• Print the result using the print() function and concatenation of string and tuple using the + operator.

## Python3

 `# Python3 code to demonstrate working of ``# Convert List to Single valued Lists in Tuple``# Using list() + tuple() + generator expression` `# initializing list``test_list ``=` `[``6``, ``8``, ``4``, ``9``, ``10``, ``2``]` `# printing original list``print``(``"The original list is: "` `+` `str``(test_list))` `# Convert List to Single valued Lists in Tuple``# Using list() + tuple() + generator expression``res_tuple ``=` `tuple``([x] ``for` `x ``in` `test_list)` `# printing result ``print``(``"Tuple after conversion: "` `+` `str``(res_tuple)) `

Output
```The original list is: [6, 8, 4, 9, 10, 2]
Tuple after conversion: ([6], [8], [4], [9], [10], [2])```

Time complexity: O(n), where n is the length of the input list, because it involves iterating through the list once.
Auxiliary space: O(n).

Previous
Next