![]() |
VOOZH | about |
Many times we can have a problem in which we need to perform interconversion between strings and in those cases, we can have a problem in which we need to convert a tuple list to raw, comma separated string. Let's discuss certain ways in which this task can be performed.
Method #1: Using str() + strip() The combination of above functions can be used to solve this problem. In this, we just convert a list into string and strip the opening, closing square brackets of list to present a string.
Step-by-step approach:
The original list is : [(1, 4), (5, 6), (8, 9), (3, 6)] Resultant string from list of tuple : (1, 4), (5, 6), (8, 9), (3, 6)
Time complexity: O(1) for initialization and printing original list, O(n) for converting list of tuples to string and printing result, where n is the length of the input list.
Auxiliary space: O(n) for creating the string representation of the input list, where n is the length of the input list.
Method #2: Using map() + join() This is yet another way in which this task can be performed. In this, we apply the string conversion function to each element and then join the tuples using join().
Step-by-step approach:
Below is the implementation of the above approach:
The original list is : [(1, 4), (5, 6), (8, 9), (3, 6)] Resultant string from list of tuple : (1, 4), (5, 6), (8, 9), (3, 6)
Time complexity: O(n), where n is the number of tuples in the list.
Auxiliary space: O(n), for the new string created by joining the tuples.
Method #3 : Another approach is to use a for loop to iterate over the elements in the list of tuples, convert each tuple to a string, and concatenate the strings together using the + operator.
Below is the implementation:
The original list is: [(1, 4), (5, 6), (8, 9), (3, 6)] Resultant string from list of tuples: (1, 4), (5, 6), (8, 9), (3, 6)
Time complexity: O(n), where n is the number of tuples in the list.
Auxiliary space: O(m), where m is the total length of the string representation of the tuples in the list.
Method #4: Using list comprehension and join()
Use a list comprehension to iterate over each tuple in the list and convert it to a string using the str() function. The resulting list of strings is then joined using the join() method with ', ' as the separator. The resulting string is stored in the variable res and printed out.
Resultant string from list of tuple : (1, 4), (5, 6), (8, 9), (3, 6)
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #5: Using a generator expression and join()
This method uses a generator expression instead of a list comprehension. This can be useful when dealing with large datasets as it avoids creating a new list in memory.
Follow the below steps to implement the above idea:
Below is the implementation of the above approach:
Resultant string from list of tuple : (1, 4), (5, 6), (8, 9), (3, 6)
Time complexity: O(n), where n is the number of tuples in the input list.
Auxiliary space: O(1), because it does not create any new lists or data structures in memory.
Method #6: Using reduce() from functools module
The reduce() function takes a function and a sequence of values and returns a single value by applying the function cumulatively to the sequence's items from left to right. In this case, the lambda function takes two tuples, converts them to strings, and concatenates them with a comma and a space.
Resultant string from list of tuple: (1, 4), (5, 6), (8, 9), (3, 6)
Time complexity: O(n), where n is the number of tuples in the list. This is because the reduce() function iterates over each tuple in the list once.
Auxiliary space: O(1), which is constant space. This is because the reduce() function only stores the intermediate results (i.e., the concatenated string) and not the entire list of tuples or any other additional data structure.
Method #7: Using numpy():
Algorithm:
Output:
The original list is: [(1, 4), (5, 6), (8, 9), (3, 6)] Resultant string from list of tuple: [[1, 4], [5, 6], [8, 9], [3, 6]]
Time complexity:
Creating a list of tuples takes O(n) time complexity, where n is the number of tuples in the list.
Converting the list to a NumPy array takes O(n) time complexity as well.
Converting the NumPy array to a string takes O(1) time complexity.
Thus, the overall time complexity of the algorithm is O(n).
Space complexity:
The space complexity of the algorithm is O(n) as we are storing the list of tuples in memory as well as the NumPy array. The space required for the resultant string is negligible compared to the size of the input list.