Python | Convert a list of characters into a string
Last Updated :
21 Mar, 2023
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
def convert(s):
new = ""
for x in s:
new + = x
return new
s = [ 'g' , 'e' , 'e' , 'k' , 's' , 'f' , 'o' , 'r' , 'g' , 'e' , 'e' , 'k' , 's' ]
print (convert(s))
|
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
def convert(s):
str1 = ""
return (str1.join(s))
s = [ 'g' , 'e' , 'e' , 'k' , 's' , 'f' , 'o' , 'r' , 'g' , 'e' , 'e' , 'k' , 's' ]
print (convert(s))
|
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:
- Import the functools module.
- Define a function called convert that takes a list of characters s as input.
- 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.
- Return the string str1.
- Define a list of characters s containing 13 elements.
- Call the convert() function with the list s as input.
- Print the result, which is the concatenated string of all the elements in s.
Below is the implementation of the above approach:
Python3
import functools
def convert(s):
str1 = functools. reduce ( lambda x,y : x + y, s)
return str1
s = [ 'g' , 'e' , 'e' , 'k' , 's' , 'f' , 'o' , 'r' , 'g' , 'e' , 'e' , 'k' , 's' ]
print (convert(s))
|
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)
|
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)
|
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’.
Please Login to comment...