Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python - Convert List of Integers to a List of Strings
Next article icon

Python | Convert List of String List to String List

Last Updated : 10 May, 2023
Comments
Improve
Suggest changes
Like Article
Like
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.



Next Article
Python - Convert List of Integers to a List of Strings
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python Programs
  • Python list-programs
Practice Tags :
  • python

Similar Reads

  • Python | Convert List of lists to list of Strings
    Interconversion of data is very popular nowadays and has many applications. In this scenario, we can have a problem in which we need to convert a list of lists, i.e matrix into list of strings. Let's discuss certain ways in which this task can be performed. Method #1 : Using list comprehension + joi
    4 min read
  • Python - Convert List of Integers to a List of Strings
    We are given a list of integers and our task is to convert each integer into its string representation. For example, if we have a list like [1, 2, 3] then the output should be ['1', '2', '3']. In Python, there are multiple ways to do this efficiently, some of them are: using functions like map(), re
    3 min read
  • Convert list of strings to list of tuples in Python
    Sometimes we deal with different types of data types and we require to inter-convert from one data type to another hence interconversion is always a useful tool to have knowledge. This article deals with the converse case. Let's discuss certain ways in which this can be done in Python. Method 1: Con
    5 min read
  • Convert List of Tuples to List of Strings - Python
    The task is to convert a list of tuples where each tuple contains individual characters, into a list of strings by concatenating the characters in each tuple. This involves taking each tuple, joining its elements into a single string, and creating a new list containing these strings. For example, gi
    3 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 passe
    5 min read
  • Python - Convert String to List of dictionaries
    Given List of dictionaries in String format, Convert into actual List of Dictionaries. Input : test_str = ["[{'Gfg' : 3, 'Best' : 8}, {'Gfg' : 4, 'Best' : 8}]"] Output : [[{'Gfg': 3, 'Best': 8}, {'Gfg': 4, 'Best': 8}]] Explanation : String converted to list of dictionaries. Input : test_str = ["[{'G
    4 min read
  • Python - List of float to string conversion
    When working with lists of floats in Python, we may often need to convert the elements of the list from float to string format. For example, if we have a list of floating-point numbers like [1.23, 4.56, 7.89], converting them to strings allows us to perform string-specific operations or output them
    3 min read
  • Python | Convert String list to Joined Single element
    Sometimes, while working with Python, we can have a problem in which we need to perform the task of joining each element of String list to a single element in List by combining using delim. This kind of application can come in web development domain. Lets discuss certain ways in which this task can
    4 min read
  • Python | Convert String ranges to list
    Sometimes, while working in applications we can have a problem in which we are given a naive string that provides ranges separated by a hyphen and other numbers separated by commas. This problem can occur across many places. Let's discuss certain ways in which this problem can be solved. Method #1:
    6 min read
  • Python - Convert a String Representation of a List into a List
    Sometimes we may have a string that looks like a list but is just text. We might want to convert that string into a real list in Python. The easiest way to convert a string that looks like a list into a list is by using the json module. This function will evaluate the string written as JSON list as
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences