Python | Convert List of String List to String List
Last Updated :
10 May, 2023
Sometimes while working in Python, we can have problems of the interconversion of data. This article talks about the conversion of list of List Strings to joined string list. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using map() + generator expression + join() + isdigit()
This task can be performed using a combination of the above functions. In this, we join the numbers using join and construct a string integers. The map() is used to apply logic to each element in list.
Python3
test_list = [[ "g" , "f" , "g" ], [ "i" , "s" ], [ "b" , "e" , "s" , "t" ]]
print ( "The original list : " + str (test_list))
res = [''.join(ele) for ele in test_list]
print ( "The String of list is : " + str (res))
|
Output :
The original list : ['[1, 4]', '[5, 6]', '[7, 10]']
List after performing conversion : ['14', '56', '710']
Time complexity: O(nm), where n is the number of sub-lists in the input list and m is the maximum length of any sub-list.
Auxiliary space: O(nm).
Method #2: Using eval() + list comprehension
The combination of above functionalities can be used to perform this task. In this, eval() interprets each strings as list and then we can convert that list to strings using join(). List comprehension is used to iterate through the list.
Python3
test_list = [[ "g" , "f" , "g" ], [ "i" , "s" ], [ "b" , "e" , "s" , "t" ]]
print ( "The original list : " + str (test_list))
res = list ( map (''.join, test_list))
print ( "The String of list is : " + str (res))
|
Output :
The original list : ['[1, 4]', '[5, 6]', '[7, 10]']
List after performing conversion : ['14', '56', '710']
The time complexity is O(nm), where n is the number of lists in the input list and m is the maximum length of the lists.
The Auxiliary space is also O(nm).
Method #3: Using enumerate function
Python3
test_list = [ "[1, 4]" , "[5, 6]" , "[7, 10]" ]
res = [''.join( str (b) for b in eval (a)) for i, a in enumerate (test_list)]
print (res)
|
Output
['14', '56', '710']
The time complexity is O(n), where n is the number of elements in the input list test_list.
The auxiliary space is O(n)
Method #4: Using for loop
Python3
test_list = [[ "g" , "f" , "g" ], [ "i" , "s" ], [ "b" , "e" , "s" , "t" ]]
print ( "The original list : " + str (test_list))
res = []
for sublist in test_list:
res.append(''.join(sublist))
print ( "The String of list is : " + str (res))
|
Output
The original list : [['g', 'f', 'g'], ['i', 's'], ['b', 'e', 's', 't']]
The String of list is : ['gfg', 'is', 'best']
Time Complexity: O(n)
Auxiliary Space: O(n)
Method#5: Using Recursive method.
Algorithm:
- Define a function named convert_to_strings which takes a nested list as input.
- Initialize an empty list named res.
- Loop through each element in the input list.
- If the element is a list, recursively call the function convert_to_strings with the element as input and append the returned string to the res list.
- If the element is not a list, append the element to the res list.
- Finally, join all the elements in the res list to form a single string and return it.
- Initialize a test list.
- Loop through each sublist in the test list and call the convert_to_strings function with the sublist as input.
- Append the returned string to the res list.
- Print the res list.
Python3
def convert_to_strings(nested_list):
res = []
for element in nested_list:
if type (element) = = list :
res.append(convert_to_strings(element))
else :res.append(element)
return ''.join(res)
test_list = [[ "g" , "f" , "g" ], [ "i" , "s" ], [ "b" , "e" , "s" , "t" ]]
print ( "The original list : " + str (test_list))
res = []
for sublist in test_list:
res.append(convert_to_strings(sublist))
print ( "The String of list is : " + str (res))
|
Output
The original list : [['g', 'f', 'g'], ['i', 's'], ['b', 'e', 's', 't']]
The String of list is : ['gfg', 'is', 'best']
Time Complexity: The time complexity of the recursive function convert_to_strings is O(n), where n is the total number of elements in the nested list. Since we are looping through each element in the list only once, the time complexity of the entire program is also O(n).
Auxiliary Space: The space complexity of the program is also O(n), where n is the total number of elements in the nested list. This is because we are creating a res list to store the result, which can contain up to n elements. Additionally, the recursive function call stack can also have up to n levels.
Method #6: Using list comprehension with join() method
Python3
test_list = [[ "g" , "f" , "g" ], [ "i" , "s" ], [ "b" , "e" , "s" , "t" ]]
res = [''.join(sublist) for sublist in test_list]
print ( "The String of list is : " + str (res))
|
Output
The String of list is : ['gfg', 'is', 'best']
Time complexity: O(n*m) where n is the number of sublists and m is the maximum length of a sublist.
Auxiliary space: O(n) where n is the number of sublists, for storing the output list.
Method 7: Using a stack data structure
Step-by-step approach:
- Define a function named convert_to_strings that takes a nested list as input.
- Initialize an empty list called res.
- Loop through each element in the nested list using a for loop.
- Check if the current element is a list using the type() function. If it is a list, call the convert_to_strings function recursively with the current element as input and append the result to the res list.
- If the current element is not a list, append it to the res list.
- After all the elements have been processed, join the elements in the res list using the join() method to create a string and return the string from the function.
- Define a nested list called test_list with some test data.
- Initialize an empty list called res.
- Loop through each sublist in the test_list using a for loop.
- Call the convert_to_strings function with the current sublist as input and append the result to the res list.
- Print the original list and the result in the required format.
Python3
def convert_to_strings(nested_list):
res = []
for element in nested_list:
if type (element) = = list :
res.append(convert_to_strings(element))
else :
res.append(element)
return ''.join(res)
test_list = [[ "g" , "f" , "g" ], [ "i" , "s" ], [ "b" , "e" , "s" , "t" ]]
res = []
for sublist in test_list:
res.append(convert_to_strings(sublist))
print ( "The original list : " + str (test_list))
print ( "The String of list is : " + str (res))
|
Output
The original list : [['g', 'f', 'g'], ['i', 's'], ['b', 'e', 's', 't']]
The String of list is : ['gfg', 'is', 'best']
Time complexity: O(n), where n is the total number of elements in the nested list.
Auxiliary space: O(d), where d is the maximum depth of the nested list.
Please Login to comment...