Python program to convert a list to string
Given a list, write a Python program to convert the given list to string. There are various situations we might encounter when a list is given and we convert it to string. For example, conversion to string from the list of string or the list of integer.
Example:
Input: ['Geeks', 'for', 'Geeks'] Output: Geeks for Geeks Input: ['I', 'want', 4, 'apples', 'and', 18, 'bananas'] Output: I want 4 apples and 18 bananas
Let’s see various ways we can convert the list to string.
Method #1: Iterate through the list and keep adding the element for every index in some empty string.
Python3
# Python program to convert a list to string # Function to convert def listToString(s): # initialize an empty string str1 = "" # traverse in the string for ele in s: str1 + = ele # return string return str1 # Driver code s = [ 'Geeks' , 'for' , 'Geeks' ] print (listToString(s)) |
GeeksforGeeks
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2: Using .join() method
Python3
# Python program to convert a list # to string using join() function # Function to convert def listToString(s): # initialize an empty string str1 = " " # return string return (str1.join(s)) # Driver code s = [ 'Geeks' , 'for' , 'Geeks' ] print (listToString(s)) |
But what if the list contains both string and integer as its element. In those cases, above code won’t work. We need to convert it to string while adding to string.
Method #3: Using list comprehension
Python3
# Python program to convert a list # to string using list comprehension s = [ 'I' , 'want' , 4 , 'apples' , 'and' , 18 , 'bananas' ] # using list comprehension listToStr = ' ' .join([ str (elem) for elem in s]) print (listToStr) |
I want 4 apples and 18 bananas
The time complexity of the program is O(n), where n is the length of the list s, because it iterates over each element of the list once.
The auxiliary space complexity of the program is O(n), because it creates a new list of strings using a list comprehension.
Method #4: Using map() Use map() method for mapping str (for converting elements in list to string) with given iterator, the list.
Python3
# Python program to convert a list # to string using list comprehension s = [ 'I' , 'want' , 4 , 'apples' , 'and' , 18 , 'bananas' ] # using list comprehension listToStr = ' ' .join( map ( str , s)) print (listToStr) |
I want 4 apples and 18 bananas
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 O(n), where n is the length of the list s.
Method #5: Using enumerate function
Python3
s = [ 'I' , 'want' , 4 , 'apples' , 'and' , 18 , 'bananas' ] listToStr = ' ' .join([ str (elem) for i,elem in enumerate (s)]) print (listToStr) |
I want 4 apples and 18 bananas
Method #6: Using in operator
Python3
s = [ 'Geeks' , 'for' , 'Geeks' ] for i in s: print (i,end = " " ) |
Geeks for Geeks
Method #7: Using functools.reduce method
Python
from functools import reduce s = [ 'I' , 'want' , 4 , 'apples' , 'and' , 18 , 'bananas' ] listToStr = reduce ( lambda a, b : a + " " + str (b), s) print (listToStr) |
I want 4 apples and 18 bananas
Using str.format method:
One additional approach to convert a list to a string in Python is to use the str.format method. This method allows you to specify a string template, and then fill in the placeholder values with the elements in the list.
For example:
Python3
lst = [ 'Geeks' , 'for' , 'Geeks' ] # Convert the list to a string using str.format result = "{} {} {}" . format ( * lst) print (result) # Output: Geeks for Geeks #This code is contributed by Edula Vinay Kumar Reddy |
Geeks for Geeks
This approach has the advantage of being able to specify exactly how the elements in the list should be formatted, by using the formatting placeholders in the string template. For example, you can specify the number of decimal places for floating point numbers, or the width and alignment of the output string.
Python3
lst = [ 1.2345 , 'good' , 3.4567 ] # Convert the list to a string using str.format result = "{:.2f} {} {:.2f}" . format ( * lst) print (result) # Output: 1.23 2.35 3.46 #This code is contributed by Edula Vinay Kumar Reddy |
1.23 good 3.46
The time complexity of the above approaches will depend on the length of the list. For example, in method 1, we are iterating through the list and adding each element to a string, so the time complexity will be O(n), where n is the length of the list.
Similarly, the time complexity of other methods will also be O(n).
The space complexity of all the above methods will also be O(n) as we are creating a new string of size n to store the elements of the list.
Method #8: Using Recursion
Python3
def list_string(start,l,word): if start = = len (l): return word #base condition to return string word + = str (l[start]) + ' ' #concatenating element in list to word variable return list_string(start + 1 ,l,word) #calling recursive function #Driver code l = [ 'Geeks' , 'for' , 'Geeks' ] #defining list print (list_string( 0 ,l,'')) |
Geeks for Geeks
Please Login to comment...