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
# Python3 code to demonstrate working of
# Convert List of lists to list of Strings
# using list comprehension + join()
# initialize list
test_list = [["g", "f", "g"], ["i", "s"], ["b", "e", "s", "t"]]
# printing original list
print("The original list : " + str(test_list))
# Convert List of lists to list of Strings
# using list comprehension + join()
res = [''.join(ele) for ele in test_list]
# printing result
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
# Python3 code to demonstrate working of
# Convert List of lists to list of Strings
# using map() + join()
# initialize list
test_list = [["g", "f", "g"], ["i", "s"], ["b", "e", "s", "t"]]
# printing original list
print("The original list : " + str(test_list))
# Convert List of lists to list of Strings
# using map() + join()
res = list(map(''.join, test_list))
# printing result
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)
['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
# initialize list
test_list = [["g", "f", "g"], ["i", "s"], ["b", "e", "s", "t"]]
# printing original list
print("The original list : " + str(test_list))
res = []
for sublist in test_list:
res.append(''.join(sublist))
# printing result
print("The String of list is : " + str(res))
#This code is contributed by Vinay Pinjala.
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:
#Function to convert nested list to list of strings
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)
#initialize list
test_list = [["g", "f", "g"], ["i", "s"], ["b", "e", "s", "t"]]
#printing original list
print("The original list : " + str(test_list))
#convert nested list to list of strings recursively
res = []
for sublist in test_list:
res.append(convert_to_strings(sublist))
#printing result
print("The String of list is : " + str(res))
#This code is contributed by tvsk.
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))
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:
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))
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.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4