Convert List to String in Python using join() reduce() map()

Convert List to String in Python using join() / reduce() / map()

List:

The list is a type of container in Data Structures in Python that is used to store multiple pieces of data at the same time. In Python, unlike Sets, the list is ordered and has a definite count. The elements in a list are indexed in a definite sequence, and the indexing of a list begins with 0 as the first index.

String:

Strings are sequences of bytes in Python that represent Unicode characters. However, since Python lacks a character data form, a single character is simply a one-length string. Square brackets may be used to access the string’s components. In Python, single quotes, double quotes, and even triple quotes can be used to generate strings.

This article will go over various methods to convert list to string.

Examples:

Input:

givenlist=["this" , "is" , "Btech" ,"Geeks"]

Output:

this is btech Geeks

List to String conversion

There are several ways to convert list to string some of them are:

Method #1 : Using String Concatenation

The elements of the input list are iterated one by one and added to a new empty string in this process.

As a result, a list is converted to a string.

Below is the implementation:

# Function which converts list to string
def listToString(givenlist):
    # Taking eempty string say string
    string = ""
    # Traverse the list and concate the elements to the string
    for element in givenlist:
        # converting element of list to string and concating to empty string
        # concatenating space to differentiate words(deliminator)
        string = string + str(element) + " "

    # return the final string
    return string


# Driver code
# given list
givenlist = ["hello", "this", "is", "Btech", "Geeks"]

# passing list to convert into string
print(listToString(givenlist))

Output:

hello this is Btech Geeks

Method #2:Using join()

The join() method in Python can be used to convert a List to a String.

Iterables such as Lists, Tuples, Strings, and others are accepted as parameters for the join() process.

It also returns a new string as an argument that contains the elements concatenated from the iterable.

The passed iterable must contain string elements as a prerequisite for the join() method to work.

A TypeError exception is thrown if the iterable includes an integer.

# Function which converts list to string
def listToString(givenlist):
  # using  space " " as separator between the elements of list
    string = ' '.join(givenlist)

    # return the final string
    return string


# Driver code
# given list
givenlist = ["hello", "this", "is", "Btech", "Geeks"]


# passing list to convert into string
print(listToString(givenlist))

Output:

hello this is Btech Geeks

Method #3:Using map()

The map() function in Python can be used to convert a list to a string.

The map() function accepts the function as well as iterable objects like lists, tuples, strings, and so on. Moving on, the map() function uses the provided function to map the elements of the iterable.

General syntax of map:

map(function, iterable)

# Function which converts list to string
def listToString(givenlist):
  # using  space " " as separator between the elements of list
    string = ' '.join(map(str, givenlist))

    # return the final string
    return string


# Driver code
# given list
givenlist = ["hello", "this", "is", "Btech", "Geeks"]


# passing list to convert into string
print(listToString(givenlist))

Output:

hello this is Btech Geeks

The map(str, givenlist) function in the preceding code snippet accepts str function and given list as arguments.

It maps each element of the input iterable( list) to the given function and returns the resultant list of elements.

In addition, the join() method is used to convert the output to string form.

Note: This method also applicable to list of integers since every element is converted to list.

Method #4:Using reduce()

Python’s functools module includes the function reduce(), which takes an iterable sequence as an argument and a function as an argument. This function returns a single value from an iterable sequence of items.

It will generate the value by passing the first two values to the given function argument and then calling the same function again with the result and the next argument. When it has consumed all of the items in order, the final result value will be returned.

Below is the implementation:

import functools
# Function which converts list to string


def listToString(givenlist):
    # deliminator or separator between the elements of list let us say it as space
    deliminator = " "
   
    # using reduce function
    string = functools.reduce(lambda a, b: a + deliminator + b, givenlist)

    # return the final string
    return string


# Driver code
# given list
givenlist = ["hello", "this", "is", "Btech", "Geeks"]


# passing list to convert into string
print(listToString(givenlist))

Output:

hello this is Btech Geeks

We passed two arguments to the reduce() function in this case,

  • A lambda function that takes two arguments and joins them with a delimiter in between.
  • Givenlist

Using the logic provided by the lambda function, it joined all of the elements in the list to form a string.
Related Programs: