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 listAuxiliary 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.
Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills, become a part of the hottest trend in the 21st century.Dive into the future of technology - explore the
Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
29 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Add Your Comment
Please Login to comment...