![]() |
VOOZH | about |
Sometimes, while working with records, we can have a problem in which any element of record can be of type string but mistakenly processed as list of characters. This can be a problem while working with a lot of data. Let's discuss certain ways in which this problem can be solved.
Method #1: Using list comprehension + join()
The combination of above functionalities can be used to achieve the solution of above task. In this, we get the character list using list comprehension and conversion task is performed by join().
The original list : [(['g', 'f', 'g'], 1), (['i', 's'], 2), (['b', 'e', 's', 't'], 3)] The joined character list tuple element to string is : ['gfg', 'is', 'best']
Time complexity: O(n * m), where n is the length of the input list and m is the maximum length of any tuple in the list.
Auxiliary Space: O(n *m), as the new list created by the list comprehension has the same length and maximum element size as the input list.
Method #2: Using map() + join() + lambda The task performed by list comprehension in the above method can be performed by map() and lambda function can be used to construct the logic to achieve the solution to this task.
The original list : [(['g', 'f', 'g'], 1), (['i', 's'], 2), (['b', 'e', 's', 't'], 3)] The joined character list tuple element to string is : ['gfg', 'is', 'best']
The time complexity of this code is O(n), where n is the length of the input list test_list.
The space complexity of this code is O(n), where n is the length of the input list test_list.
Method #3: Using a for loop
Iterates over each tuple in test_list, joins its first element (a list of strings), and appends the resulting string to the res list. Finally, it prints the result.
The joined character list tuple element to string is : ['gfg', 'is', 'best']
The time complexity of this code is O(n*m), where n is the length of the test_list and m is the maximum length of the sub-lists in test_list.
The space complexity of this code is also O(nm), because it creates a new list res to store the concatenated strings.
Method 4: Using a generator expression
We can also solve this problem using a generator expression, which is similar to a list comprehension but doesn't create a list in memory. This can be more memory-efficient for large input lists.
Follow the below steps to implement the above idea:
The joined character list tuple element to string is : ['gfg', 'is', 'best']
Time Complexity: O(n*m).where n is the number of tuples in test_list and m is the maximum length of the string list in each tuple.
Auxiliary Space: O(n*m).where n is the number of tuples in test_list and m is the maximum length of the string list in each tuple.
Method 5: Using reduce() function
The original list : [(['g', 'f', 'g'], 1), (['i', 's'], 2), (['b', 'e', 's', 't'], 3)] The joined character list tuple element to string is : ['gfg', 'is', 'best']
Time complexity: O(n), where n is the number of tuples in the input list.
Auxiliary space: O(n), where n is the number of tuples in the input list.
Method 6: Using a nested for loop and join()
Step-by-step approach :
The joined character list tuple element to string is : ['gfg', 'is', 'best']
Time complexity: O(n^2) where n is the length of the input list test_list.
Auxiliary space: O(n), as we create a new list res to store our results, which can be of size n at most.