Open In App

Python | Convert a list of characters into a string

Last Updated : 21 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a list of characters, merge all of them into a string. Examples:

Input : ['g', 'e', 'e', 'k', 's', 'f', 'o', 
             'r', 'g', 'e', 'e', 'k', 's']
Output : geeksforgeeks 

Input : ['p', 'r', 'o', 'g', 'r', 'a', 'm', 
                        'm', 'i', 'n', 'g']
Output : programming 

Initialize an empty string at the beginning. Traverse in the list of characters, for every index add character to the initial string. After complete traversal, print the string which has been added with every character. 

Implementation:

Python




# Python program to convert a list
# of character
 
def convert(s):
 
    # initialization of string to ""
    new = ""
 
    # traverse in the string
    for x in s:
        new += x
 
    # return string
    return new
     
     
# driver code
s = ['g', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's']
print(convert(s))


Output

geeksforgeeks

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

Method #2: Using join() function

By using join() function in python, all characters in the list can be joined. The syntax is:

str = ""
str1 = ( "geeks", "for", "geeks" )
str.join(str1) 

The list of characters can be joined easily by initializing str=”” so that there are no spaces in between. 

Implementation:

Python




# Python program to convert a list
# of character
 
def convert(s):
 
    # initialization of string to ""
    str1 = ""
 
    # using join function join the list s by
    # separating words by str1
    return(str1.join(s))
     
# driver code
s = ['g', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's']
print(convert(s))


Output

geeksforgeeks

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

Method #3: Using reduce() function

Reduce function is used to convert the iterables to reduce in a single cumulative value. 

Follow the below steps to implement the above idea:

  1. Import the functools module.
  2. Define a function called convert that takes a list of characters s as input.
  3. Use the reduce() function from the functools module to concatenate the elements of the list s into a single string str1. The lambda function passed to reduce() takes two arguments x and y, and concatenates them.
  4. Return the string str1.
  5. Define a list of characters s containing 13 elements.
  6. Call the convert() function with the list s as input.
  7. Print the result, which is the concatenated string of all the elements in s.

Below is the implementation of the above approach:

Python3




# Python program to convert a list
# of character
import functools
 
def convert(s):
 
    # Using reduce to join the list s to string
    str1 = functools.reduce(lambda x,y : x+y, s)
 
    # Return string 1
    return str1
     
# driver code
s = ['g', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's']
print(convert(s))


Output

geeksforgeeks

Time Complexity: O(n), where n is the length of the list.
Auxiliary Space: O(n), where n is the length of the string generated by the reduce function.

Method #4: Using list comprehension 

Python3




s=['g','e','e','k','s','f','o','r','g','e','e','k']
x="".join([str(i) for i in s])
print(x)


Output

geeksforgeek

Time Complexity: O(n), where n is the length of the list.
Auxiliary Space: O(n), where n is the length of the string generated.

Method #5: Using enumerate function 

Python3




s=['g','e','e','k','s','f','o','r','g','e','e','k']
x="".join([str(i) for a,i in enumerate(s)])
print(x)


Output

geeksforgeek

The time complexity of this code is O(n), where n is the length of the list ‘s’.

The space complexity of this code is also O(n), since the string ‘x’ will take up additional space in memory that is proportional to the size of the list ‘s’. 



Similar Reads

Python | Convert list of strings and characters to list of characters
Sometimes we come forward to the problem in which we receive a list that consists of strings and characters mixed and the task we need to perform is converting that mixed list to a list consisting entirely of characters. Let's discuss certain ways in which this is achieved. Method #1 : Using List comprehension In this method, we just consider each
6 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 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 combinat
6 min read
Count of ungrouped characters after dividing a string into K groups of distinct characters
Given a lower alphabetic string "S" of size N, and an integer K; the task is to find the count of characters that will remain ungrouped, after dividing the given string into K groups of distinct characters.Examples: Input: S = "stayinghomesaveslife", K = 1 Output: 6 Explanation: In the string S the elements whose occurrence is more than one time ar
6 min read
Convert a String into a square matrix grid of characters
Given a string of length L. The task is to convert the string into a grid.Examples: Input : str = "haveaniceday" Output : have anic eday Explanation: k is the separator. If k is 4 then the output will be "have anic eday" Input :str = "geeksforgeeks" Output : geek sfor geek s Note: & l = length of the string Approach: Without using an inbuilt fu
5 min read
Python | Split string into list of characters
Given a string, write a Python program to split the characters of the given string into a list using Python. In this article, we will explore various methods to split a string into a list of characters, allowing developers to manipulate and work with individual characters efficiently. Input: geeks Output : ['g', 'e', 'e', 'k', 's'] Input: Word Outp
4 min read
Python | Convert list of tuples into list
Given a list of tuples, write a Python program to convert it into a list. Example Input: [(11, 20), (13, 40), (55, 16)] Output:[11, 20, 13, 40, 55, 16] Input: [('Geeks', 2), ('For', 4), ('geek', '6')] Output: ['Geeks', 2, 'For', 4, 'geek', '6'] Convert List of Tuples to List of Lists in Python MethodsConvert lists of tuples into a list using LoopTh
5 min read
Python | Convert list into list of lists
Given a list of strings, write a Python program to convert each element of the given list into a sublist. Thus, converting the whole list into a list of lists. Examples: Input : ['alice', 'bob', 'cara'] Output : [['alice'], ['bob'], ['cara']] Input : [101, 202, 303, 404, 505] Output : [[101], [202], [303], [404], [505]] Approach #1 : Naive Approach
5 min read
Python | Ways to Convert a 3D list into a 2D list
List is a common type of data structure in Python. While we have used the list and 2d list, the use of 3d list is increasing day by day, mostly in case of web development. Given a 3D list, the task is to convert it into a 2D list. These type of problems are encountered while working on projects or while contributing to open source. Below are some w
3 min read