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 String ranges to list
Next article icon

Convert List to Delimiter Separated String - Python

Last Updated : 08 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The task of converting a list to a delimiter-separated string in Python involves iterating through the list and joining its elements using a specified delimiter. For example, given a list a = [7, "Gfg", 8, "is", "best", 9] and a delimiter "*", the goal is to produce a single string where each element is converted to a string and separated by "*", resulting in "7*Gfg*8*is*best*9".

Using str.join()

str.join() is the most efficient way to convert a list into a delimiter-separated string. It works by first converting each element into a string and then joining them using the specified delimiter. This method is highly optimized for performance and is the preferred choice in most cases.

Python
a = [7, "Gfg", 8, "is", "best", 9]   delim = "*"  res = delim.join(map(str, a)) print(res) 

Output
7*Gfg*8*is*best*9 

Explanation: map(str, a) converts each element of a into a string since integers cannot be directly joined and delim.join(...) joins all converted string elements using * as a separator.

Table of Content

  • Using list comprehension
  • Using reduce()
  • Using for loop

Using list comprehension

List comprehension allows transforming each element into a string before joining them, making it useful when additional modifications are needed. While slightly less efficient than map(str, a), it remains a clean and readable alternative to str.join().

Python
a = [7, "Gfg", 8, "is", "best", 9]    res = "*".join([str(ele) for ele in a]) print(res) 

Output
7*Gfg*8*is*best*9 

Explanation: [str(ele) for ele in a] converts each element of a into a string, and "*".join(...) joins all converted elements using * as a separator, creating a single formatted string.

Using reduce()

reduce() from the functools module applies a binary function iteratively to combine all elements into a single string. Though functional in nature, it is less efficient than str.join() due to repeated string creation.

Python
from functools import reduce a = [7, "Gfg", 8, "is", "best", 9]    res = reduce(lambda x, y: str(x) + "*" + str(y), a) print(res) 

Output
7*Gfg*8*is*best*9 

Explanation: reduce() applies the lambda function cumulatively to the list elements, where lambda x, y: str(x) + "*" + str(y) converts x and y to strings, concatenates them with * as a separator and continues this process until all elements are merged into a single string.

Using for loop

A traditional for loop can manually concatenate elements into a string. Although simple and intuitive, this approach is inefficient due to repeated immutable string operations, making it slower for large lists.

Python
a = [7, "Gfg", 8, "is", "best", 9]   res = ""  for ele in a:     res += str(ele) + "*" res = res[:-1] print(res) 

Output
7*Gfg*8*is*best*9 

Explanation: for loop converts each element to a string, appends it to res with * and res[:-1] removes the trailing * for proper formatting.


Next Article
Python | Convert String ranges to list
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python Programs
  • Python list-programs
  • Python string-programs
Practice Tags :
  • python

Similar Reads

  • Python - Convert delimiter separated Mixed String to valid List
    Given a string with elements and delimiters, split elements on delimiter to extract with elements ( including containers). Input : test_str = "6*2*9*[3, 5, 6]*(7, 8)*8*4*10", delim = "*" Output : [6, 2, 9, [3, 5, 6], (7, 8), 8, 4, 10] Explanation : Containers and elements separated using *. Input :
    10 min read
  • Python - Convert Delimiter separated list to Number
    Given a String with delimiter separated numbers, concatenate to form integer after removing delimiter. Input : test_str = "1@6@7@8", delim = '@' Output : 1678 Explanation : Joined elements after removing delim "@"Input : test_str = "1!6!7!8", delim = '!' Output : 1678 Explanation : Joined elements a
    6 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 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() + isd
    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 passe
    5 min read
  • How To Convert Comma-Delimited String to a List In Python?
    In Python, converting a comma-separated string to a list can be done by using various methods. In this article, we will check various methods to convert a comma-delimited string to a list in Python. Using str.split()The most straightforward and efficient way to convert a comma-delimited string to a
    1 min read
  • Python - Sort words separated by Delimiter
    Given string of words separated by some delimiter. The task is to sort all the words given in the string Input : test_str = 'gfg:is:best:for:geeks', delim = "*" Output : best*for*geeks*gfg*is Explanation : Words sorted after separated by delim. Input : test_str = 'gfg:is:best', delim = "*" Output :
    6 min read
  • Python | Delimited String List to String Matrix
    Sometimes, while working with Python strings, we can have problem in which we need to convert String list which have strings that are joined by deliminator to String Matrix by separation by deliminator. Lets discuss certain ways in which this task can be performed. Method #1 : Using loop + split() T
    5 min read
  • Input a comma separated string - Python
    Handling comma-separated input in Python involves taking user input like '1, 2, 3' and converting it into usable data types such as integers or floats. This is especially useful when dealing with multiple values entered in a single line. Let's explore different efficient methods to achieve this: Usi
    3 min read
  • Python | Convert heterogeneous type String to List
    Sometimes, while working with data, we can have a problem in which we need to convert data in string into a list, and the string contains elements from different data types like boolean. This problem can occur in domains in which a lot of data types are used. Let's discuss certain ways in which this
    6 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