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

Python | Convert String list to Joined Single element

Last Updated : 08 May, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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 be performed. 

Method #1 : Using loop

This is one of way to perform this task. In this, we iterate for the String list for strings using loop and perform the join operation according to the delim. 

Python3




# Python3 code to demonstrate working of
# Convert String list to Joined Single element
# Using loop
 
# initializing list
test_list = ['gfg', 'is', 'best']
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing delim
delim = "-"
 
# Convert String list to Joined Single element
# Using loop
res = ''
for idx in range(len(test_list)-1):
    res = res + test_list[idx] + delim
 
  # if list is greater than 0
if len(test_list) > 0:
    res = res + test_list[-1]
res = [res]
 
# printing result
print("String after performing join : " + str(res))
 
 
Output : 
The original list is : ['gfg', 'is', 'best'] String after performing join : ['gfg-is-best']

Time Complexity: O(n), where n is the length of the input list. This is because we’re using the loop which has a time complexity of O(n) in the worst case.
Auxiliary Space: O(n), as we’re using additional space res other than the input list itself with the same size of input list. 

Method #2: Using join() + list comprehension

This task can also be performed using a combination of the above functions. In this, we perform the task of combining using join(). The logic is compiled using list comprehension.

Python3




# Python3 code to demonstrate working of
# Convert String list to Joined Single element
# Using join() + list comprehension
 
# initializing list
test_list = ['gfg', 'is', 'best']
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing delim
delim = "-"
 
# Convert String list to Joined Single element
# Using join() + list comprehension
res = [delim.join(test_list)]
 
# printing result
print("String after performing join : " + str(res))
 
 
Output : 
The original list is : ['gfg', 'is', 'best'] String after performing join : ['gfg-is-best']

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #3 : Using reduce() + add()

This method makes use of the reduce function from functools module to apply a binary function cumulatively on the elements of the given list. The binary function used here is add() from operator module.

Python3




# Python3 code to demonstrate working of
# Convert String list to Joined Single element
# Using reduce() + add()
 
# importing reduce from functools
from functools import reduce
 
# importing add from operator
from operator import add
 
# initializing list
test_list = ['gfg', 'is', 'best']
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing delim
delim = "-"
 
# Convert String list to Joined Single element
# Using reduce() + add()
res = reduce(add, [x + delim for x in test_list[:-1]] + [test_list[-1]])
res = [res]
 
# printing result
print("String after performing join : " + str(res))
 
 
Output
The original list is : ['gfg', 'is', 'best'] String after performing join : ['gfg-is-best']

Time Complexity: O(n), where n is the number of elements in the list.
Auxiliary Space: O(n), where n is the length of the resulting string after join operation.

Method 4 : Using itertools.chain() and join() functions

Python3




import itertools
 
# initializing list
test_list = ['gfg', 'is', 'best']
 
# initializing delimiter
delim = "-"
 
# using itertools.chain() to concatenate the elements of the list with delimiter
concatenated_list = itertools.chain.from_iterable(
    (s, delim) for s in test_list[:-1])
 
# concatenating the concatenated list with the last element of the original list
joined_string = ''.join(concatenated_list) + test_list[-1]
 
# creating a list with the joined string as the only element
res = [joined_string]
 
# printing result
print("String after performing join: " + str(res))
 
 
Output
String after performing join: ['gfg-is-best']

Time complexity: O(n), where n is the length of the list.
Auxiliary space: O(n), as we are creating an intermediate concatenated list of length n.



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

Similar Reads

  • 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
  • Convert List to Delimiter Separated String - Python
    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 elemen
    3 min read
  • Convert String Float to Float List in Python
    We are given a string float we need to convert that to float of list. For example, s = '1.23 4.56 7.89' we are given a list a we need to convert this to float list so that resultant output should be [1.23, 4.56, 7.89]. Using split() and map()By using split() on a string containing float numbers, we
    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
  • Convert Dictionary to String List in Python
    The task of converting a dictionary to a string list in Python involves transforming the key-value pairs of the dictionary into a formatted string and storing those strings in a list. For example, consider a dictionary d = {1: 'Mercedes', 2: 'Audi', 3: 'Porsche', 4: 'Lambo'}. Converting this to a st
    3 min read
  • Python - Convert case of elements in a list of strings
    In Python, we often need to convert the case of elements in a list of strings for various reasons such as standardizing input or formatting text. Whether it's converting all characters to uppercase, lowercase, or even swapping cases. In this article, we'll explore several methods to convert the case
    3 min read
  • Convert Character Matrix to single String - Python
    In this article, we will explore various methods to convert character matrix to single string in Python. The simplest way to do is by using a loop. Using a LoopWe can use a loop (for loop) to iterate through each sublist and concatenate the elements of each sublist and then combine them into a final
    2 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
  • Convert List Of Tuples To Json String in Python
    We have a list of tuples and our task is to convert the list of tuples into a JSON string in Python. In this article, we will see how we can convert a list of tuples to a JSON string in Python. Convert List Of Tuples To Json String in PythonBelow, are the methods of Convert List Of Tuples To Json St
    3 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
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