Open In App

Python | Convert List of String List to String List

Last Updated : 10 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.



Similar Reads

Python | Convert list of string to list of list
Many times, we come over the dumped data that is found in the string format and we require it to be represented in the actual list format in which it was actually found. This kind of problem of converting a list represented in string format back to la ist to perform tasks is quite common in web development. Let's discuss certain ways in which this
7 min read
Python | Convert a string representation of list into list
Many times, we come across the dumped data that is found in the string format and we require it to be represented in the actual list format in which it was actually found. This kind of problem of converting a list represented in string format back to a list in Python to perform tasks is quite common in web development. Convert a string of a list in
6 min read
Python | Convert list of string into sorted list of integer
Given a list of string, write a Python program to convert it into sorted list of integer. Examples: Input: ['21', '1', '131', '12', '15'] Output: [1, 12, 15, 21, 131] Input: ['11', '1', '58', '15', '0'] Output: [0, 1, 11, 15, 58] Let's discuss different methods we can achieve this task. Method #1: Using map and sorted() C/C++ Code # Python code to
4 min read
Python | Convert list of numerical string to list of Integers
Many times, the data we handle might not be in the desired form for any application and has to go through the stage of preprocessing. One such kind of form can be a number in the form of a string that too is a list in the list and we need to segregate it into digit-separated integers. Let's discuss certain ways in Python in which this problem can b
6 min read
Python | Convert string enclosed list to list
Given a list enclosed within a string (or quotes), write a Python program to convert the given string to list type. Examples: Input : "[0, 2, 9, 4, 8]" Output : [0, 2, 9, 4, 8] Input : "['x', 'y', 'z']" Output : ['x', 'y', 'z'] Approach #1: Python eval() The eval() method parses the expression passed to this method and runs python expression (code)
5 min read
Python | Convert mixed data types tuple list to string list
Sometimes, while working with records, we can have a problem in which we need to perform type conversion of all records into a specific format to string. This kind of problem can occur in many domains. Let's discuss certain ways in which this task can be performed. Method #1 : Using list comprehension + tuple() + str() + generator expression The co
5 min read
Python | Convert string List to Nested Character List
Sometimes, while working with Python, we can have a problem in which we need to perform interconversion of data. In this article we discuss converting String list to Nested Character list split by comma. Let's discuss certain ways in which this task can be performed. Method #1 : Using list comprehension + split() The combination of above functional
7 min read
Python - Convert String List to Key-Value List dictionary
Given a string, convert it to key-value list dictionary, with key as 1st word and rest words as value list. Input : test_list = ["gfg is best for geeks", "CS is best subject"] Output : {'gfg': ['is', 'best', 'for', 'geeks'], 'CS': ['is', 'best', 'subject']} Explanation : 1st elements are paired with respective rest of words as list. Input : test_li
8 min read
Python Program to convert List of Integer to List of String
Given a List of Integers. The task is to convert them to a List of Strings. Examples: Input: [1, 12, 15, 21, 131]Output: ['1', '12', '15', '21', '131']Input: [0, 1, 11, 15, 58]Output: ['0', '1', '11', '15', '58']Method 1: Using map() [GFGTABS] Python3 # Python code to convert list of # string into sorted list of integer # List initialization list_i
5 min read
Python | Convert list of tuples to list of list
This is a quite simple problem but can have a good amount of application due to certain constraints of Python language. Because tuples are immutable, they are not easy to process whereas lists are always a better option while processing. Let's discuss certain ways in which we can convert a list of tuples to list of list. Method #1: Using list compr
8 min read
Practice Tags :
three90RightbarBannerImg