Monday, November 18, 2024
Google search engine
HomeLanguagesPython | Convert List of String List to String List

Python | Convert List of String List to String List

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)


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




# 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.


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:

  1. Define a function named convert_to_strings which takes a nested list as input.
  2. Initialize an empty list named res.
  3. Loop through each element in the input list.
  4. 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.
  5. If the element is not a list, append the element to the res list.
  6. Finally, join all the elements in the res list to form a single string and return it.
  7. Initialize a test list.
  8. Loop through each sublist in the test list and call the convert_to_strings function with the sublist as input.
  9. Append the returned string to the res list.
  10. Print the res list.

Python3




#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.


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:

  1. Define a function named convert_to_strings that takes a nested list as input.
  2. Initialize an empty list called res.
  3. Loop through each element in the nested list using a for loop.
  4. 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.
  5. If the current element is not a list, append it to the res list.
  6. 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.
  7. Define a nested list called test_list with some test data.
  8. Initialize an empty list called res.
  9. Loop through each sublist in the test_list using a for loop.
  10. Call the convert_to_strings function with the current sublist as input and append the result to the res list.
  11. 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.

RELATED ARTICLES

Most Popular

Recent Comments