Python - Convert List to Single Valued Lists in Tuple


This article is our approach to try and understand the various methods to convert a list to single valued lists in Tuple. We will look into the possible Python programming to achieve the same. We will discuss the implementation details, time and space complexities, and efficiency of each method based on the size of the input list. Developers, data scientists, and general readers with a basic technical background will find this article beneficial for judging the best approach for their specific use cases.

In python, converting a list to a tuple of single valued lists is not a complex task. This article will focus on five different approaches for this transformation. Remember, there are several other methods possible so keep exploring. Let’s discuss each method based on its logic of implementation, time and space complexity. And, going ahead readers will be able to judge the best approach based on their specific requirements.

Method 1: Implementing a Recursive Function

Recursion is an approach in programming where a function calls itself (with next use case), finally ending in a base case that terminates the recursive calls.

Let’s see the code:

Example

def tuple_singlelist(listInput):
    if not listInput:
        return ()
    else:
        return ([listInput[0]],) + tuple_singlelist(listInput[1:])

listDemo = [1,2,3,4,5]
output_tuple = tuple_singlelist(listDemo)

# Printing the input list and the output tuple of single lists
print("input list:", listDemo)
print("output tuple:", output_tuple)

Output

input list: [1, 2, 3, 4, 5]
output tuple: ([1], [2], [3], [4], [5])

In the recursive function, tuple_singlelist(), we take a list as an input, listInput. Then, we pass the list through an if-else statement checking if the listInput is empty. If true, it acts as a base case and exits the recursion returning an empty tuple. If false, i.e. listInput is not empty, then the first element of the listInput is taken to create a single valued list and enclosed in a tuple. Then, the function, tuple_singlelist is recalled with the input list slicing its first element, listInput[1:].

The time complexity of the recursive function is O(n2), where n is the length of the list input. The space complexity is also O(n2),where n is the length of the list input.

Method 2: Using List Comprehension and Tuple Function

One of the simplest approaches for the conversion will be the list comprehension method. We can easily use the list comprehension method to parse individual elements to form a list of single valued lists. And then, that list can be converted to a tuple. Let’s look at our code to understand:

Example

demo_list = [1,2,3,4,5]
#printing initial list
print("Initial List:"+ str(demo_list))

#Using list conversion and tuple() function
final_tuple = tuple([x] for x in demo_list)

#printing after tuple conversion
print("Final Tuple:"+ str(final_tuple))

Output

Initial List:[1, 2, 3, 4, 5]
Final Tuple:([1], [2], [3], [4], [5])

In this code, the [x] for x in the demo_list command fetches individual elements from demo_list and makes a list of the single values. Next, the tuple() function encloses all the single valued lists formed into a tuple.

The time complexity of the recursive function is O(n), where n is the length of the list input. The space complexity is also O(n), where n is the length of the list input.

Method 3: Using simple for loop, tuple() and append() function

This is a simple thought process we can build to break down this problem. A list to be broken to individual elements, then individual elements to be inside a tuple.

To achieve the same we can use an empty list where we can store our values, by appending the individual elements as a list. The iteration being performed by a for loop. Once we have our list of single valued lists, we can then implement the tuple() function to create the tuple.

Let’s dive into the code:

Example

listInput = [1,2,3,4,5]

#printing initial list
print("Initial List:"+ str(listInput))

finalList=[]
for item in listInput:
    finalList.append([item])
outputTuple = tuple(finalList)

#printing after tuple conversion
print("Final Tuple:"+ str(outputTuple))

Output

Initial List:[1, 2, 3, 4, 5]
Final Tuple:([1], [2], [3], [4], [5])

So, what we have done in the above code is, we have created an empty list. Now, a for loop is used to iterate each item in the entered list. As we discussed earlier the role of append() is here, where those individual elements are passed after closing inside square brackets[ ] to make a list of single valued lists. Later, the tuple function is used to convert the generated list to a tuple.

The time and space complexity remains the same as last method, i.e. O(n), n being the length of the input list.

Method 4: Combining map() and lambda function

Map is a very useful function in python, it helps us iterate any iterable after applying a specific instruction to each item of the iterable.

We can use lambda expressions inside the map() to achieve required tasks. Let’s write a code to try and generate a tuple of single valued lists.

Example

listInput = [1,2,3,4,5]

#printing initial list
print("Initial List:"+ str(listInput))

#implement map(lambda)
outputTuple = tuple(map(lambda ele: [ele], listInput))

#printing after tuple conversion
print("Final Tuple:"+ str(outputTuple))

Output

Initial List:[1, 2, 3, 4, 5]
Final Tuple:([1], [2], [3], [4], [5])

If we look at our code, we see that it's a simple one liner, the map function encloses lambda function, that provides instruction for each iterated element in the iterable it is to be closed inside square brackets[ ], i.e. make a list. The map function iterates each element in listInput passing through this lambda function.

Time complexity of the function is O(n) and the space complexity is also O(n).

Method 5: Creating generator expressions for tuple()

Generator expressions are somewhere similar to list comprehension, just that instead of list, a generator object is generated which can be iterated to create values.

For creating the required tuple, we can pass a generator statement, inside the tuple() function, to create a single valued list from each iterated list element. A code will make it easier for us to visualize, let’s look into the code.

Example

listInput = [1,2,3,4,5]
#printing initial list
print("Initial List:"+ str(listInput))
outputTuple = tuple([item] for item in listInput)
#printing after tuple conversion
print("Final Tuple:"+ str(outputTuple))

Output

Initial List:[1, 2, 3, 4, 5]
Final Tuple:([1], [2], [3], [4], [5])

As you can see, we have used a generator expression that creates single valued lists from each iterated item from a given list. And passing that inside tuple() gives us the desired output.

Time complexity and space complexity: O(𝑛)

Conclusion

Coming to the end of the five different methods we have implemented in this article so far, there always remains the scope to explore other existing and possible ways to implement or achieve the same functionality or output.

Python has served as a very productive language and its use in dynamic fields makes it very powerful.

Updated on: 29-Aug-2023

87 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements