Sunday, November 17, 2024
Google search engine
HomeLanguagesPython | Convert list of strings to space separated string

Python | Convert list of strings to space separated string

Given a list of strings, write a Python program to convert the given list of strings into a space-separated string. 

Examples:

Input : ['Lazyroar', 'for', 'Lazyroar']
Output : Lazyroar for Lazyroar

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




# Python3 program to Convert a list of
# strings to space-separated string
 
def convert(lst):
     
    return str(lst).translate(None, '[],\'')
     
# Driver code
lst = ['Lazyroar', 'for', 'Lazyroar']
print(convert(lst))


Output:

Lazyroar for Lazyroar

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




# Python3 program to Convert list of
# strings to space separated string
 
def convert(lst):
     
    return ' '.join(lst)
     
# Driver code
lst = ['Lazyroar', 'for', 'Lazyroar']
print(convert(lst))


Output:

Lazyroar for Lazyroar

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




# Python3 program to Convert list of
# strings to space separated string
 
def convert(lst):
    x=""
    for i in lst:
        x+=i+" "
    x.strip()
    return x
         
     
# Driver code
lst = ['Lazyroar', 'for', 'Lazyroar']
print(convert(lst))


Output

Lazyroar for Lazyroar 

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):
    # using reduce function to cumulatively apply lambda function to each element
    # in the list and concatenate them with space
    return reduce(lambda x,y: x + ' ' + y, lst)
 
#Driver code
lst = ['Lazyroar', 'for', 'Lazyroar']
print(convert(lst))
#This code is contributed by Edula Vinay Kumar Reddy


Output

Lazyroar for Lazyroar

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)
     
# Driver code
lst = ['Lazyroar', 'for', 'Lazyroar']
print(convert(lst))


Output

Lazyroar for Lazyroar

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.

RELATED ARTICLES

Most Popular

Recent Comments