Given a list of strings, write a Python program to convert the given list of strings into a space-separated string.
Examples:
Input : ['geeks', 'for', 'geeks']
Output : geeks for geeks
Input : ['Python', 'Language']
Output : Python Language
Approach #1 : Python string translate() The string translate() method returns a string where each character is mapped to its corresponding character in the translation table. The limitation of this method is that it does not work for Python 3 and above versions.
Python
def convert(lst):
return str (lst).translate( None , '[],\'' )
lst = [ 'geeks' , 'for' , 'geeks' ]
print (convert(lst))
|
Time complexity is O(n), where n is the length of the input string.
Auxiliary space: O(n) where n is the length of the input list “lst”.
Approach #2 : join() function The join() method is a string method and returns a string in which the elements of sequence have been joined by str separator. In this approach space is the separator.
Python3
def convert(lst):
return ' ' .join(lst)
lst = [ 'geeks' , 'for' , 'geeks' ]
print (convert(lst))
|
Time complexity: O(n) where n is the number of elements in the list.
Auxiliary space: O(n) as the space required is proportional to the number of elements in the list.
Approach #3: Using for loop
Step-by-step approach :
- Define a function named “convert” that takes a list as an argument.
- Initialize an empty string “x“.
- Loop through the items of the input list “lst“.
- In each iteration, concatenate the current item and space to the string “x“.
- After the loop, remove any leading or trailing whitespace from the string “x” using the strip() method.
- Return the modified string “x“.
- Define a list “lst” with three string items.
- Call the “convert” function with the “lst” as an argument.
- Print the returned string.
Below is the implementation of the above approach:
Python3
def convert(lst):
x = ""
for i in lst:
x + = i + " "
x.strip()
return x
lst = [ 'geeks' , 'for' , 'geeks' ]
print (convert(lst))
|
Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(n), where n is the length of the input list.
Approach #4 : Using reduce()
The reduce() function applies a function to an iterable, cumulatively reducing it to a single value. In this case, it applies a lambda function to each element in the list which concatenates the current element with a space and the next element.
Python3
from functools import reduce
def convert(lst):
return reduce ( lambda x,y: x + ' ' + y, lst)
lst = [ 'geeks' , 'for' , 'geeks' ]
print (convert(lst))
|
Time Complexity: O(n), where n is the number of elements in the list
Auxiliary Space: O(1) as we are not creating any new list and the concatenation is happening in place.
Method 5: Use the map() function along with the join() method.
Here’s the step-by-step approach:
- Define a function called convert that takes a list as input.
- Use the map() function with the str.strip() method to remove whitespace from each string element in the list.
- Use the join() method to join the stripped elements with a space separator.
- Return the resulting string.
Python3
def convert(lst):
stripped_list = map ( str .strip, lst)
return ' ' .join(stripped_list)
lst = [ 'geeks' , 'for' , 'geeks' ]
print (convert(lst))
|
Time complexity: O(n), where n is the length of the list.
Auxiliary space: O(n), as we are creating a new string to return. However, since strings in Python are immutable, the space complexity could be slightly higher due to the creation of temporary string objects during the execution.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
29 Mar, 2023
Like Article
Save Article