Convert list to string in python using join() / reduce() / map()

In this article we will discuss different ways to convert list to string.

Convert list to string in python using join() in python

In python string class provides a function join() i.e.

string.join(iterable)

join() function accepts an iterable sequence like list or tuple etc as an argument and then joins all items in this iterable sequence to create a string. In the end it returns the concatenated string.

Let’s use this join() function to convert list to string in python.

We have created a function that accepts a list and a delimiter as arguments returns a string by joining all the elements in that list,

def convert_list_to_string(org_list, seperator=' '):
    """ Convert list to string, by joining all item in list with given separator.
        Returns the concatenated string """
    return seperator.join(org_list)

It uses the given delimiter as separator while joining items in list. If delimiter is not provided then by default it uses a white space as delimiter. Now let’s use this function to convert a list to string,

Convert list of strings to string with space as delimiter in python

Suppose we have a list of strings,

list_of_words = ["This", "is", "a", "sample", "program"]

Convert list to string by join all the strings items in the list to create a concatenated string,

def convert_list_to_string(org_list, seperator=' '):
    """ Convert list to string, by joining all item in list with given separator.
        Returns the concatenated string """
    return seperator.join(org_list)

# Convert list of strings to string
full_str = convert_list_to_string(list_of_words)

print(full_str)

Output:

This is a sample program

As we didn’t passed any delimiter, so by default a white space character was used as separator.

Convert list to string with comma as delimiter in python

def convert_list_to_string(org_list, seperator=' '):
    """ Convert list to string, by joining all item in list with given separator.
        Returns the concatenated string """
    return seperator.join(org_list)

# Join all the strings in list
full_str = convert_list_to_string(list_of_words, ',')

print(full_str)

Output:

This is a sample program

In this example, while calling the function convert_list_to_string(), we passed a comma ‘,’ as delimiter / separator, therefore it joined all the items in list using comma as separator.

Convert list to string with custom delimiter in python

We can also use any custom delimiter while converting a list to string. For example,

def convert_list_to_string(org_list, seperator=' '):
    """ Convert list to string, by joining all item in list with given separator.
        Returns the concatenated string """
    return seperator.join(org_list)

# Join all the strings in list
full_str = convert_list_to_string(list_of_words, '---')
print(full_str)

Output:

This---is---a---sample---program

In this example, while calling the function convert_list_to_string(), we passed a comma ‘—‘ as delimiter / separator, therefore it joined all the items in list using ‘—-‘ as separator.

Convert list of integers to string in python using join() in python

Suppose we have a list of integers,

list_of_num = [1, 2, 3, 4, 5]

We want to convert this list of ints to a string by joining all the integers in list in string representation with given delimiter as separator between items. Final string should be like,

1 2 3 4 5

We can do using join() function, but we need to first convert list of ints to a list of strings and then we can join all the items in that list to create a string with separator. For example,

list_of_num = [1, 2, 3, 4, 5]

# Covert list of integers to a string
full_str = ' '.join([str(elem) for elem in list_of_num])

print(full_str)

Output:

1 2 3 4 5

We joined all the elements by using a white space as delimiter.

Convert list of different type items to string using join() in python

Suppose we have a list that contains different type of elements like int, float , strings etc,

mix_list = ["This", "is", "a", "sample", 44, 55, 66, "program"]

We want to convert this list to a string by joining all the items in list in string representation with given delimiter as separator between items. Final string should be like,

This is a sample 44 55 66 program

We can do using join() function, but we need to first convert list of different type of elements to a list of strings. For that we need to call str() on each item of the list to convert it to string. Then we can join all the items in the new list of strings to create a string.

For example,

mix_list = ["This", "is", "a", "sample", 44, 55, 66, "program"]

# Covert list of different type of items to string
full_str = ' '.join([str(elem) for elem in mix_list])

print(full_str)

Output:

This is a sample 44 55 66 program

We joined all the elements by using a white space as delimiter.

Convert list to string using reduce() in python

reduce(function, sequence[, initial])

functools module in python provides a function reduce(), which accepts a iterable sequence as argument and a function as argument. This function generates a single value from all the items in given iterable sequence. For generating the value it will, pass the first two values to given function argument and then keeps on calling the same function with result and the next argument. When it consumes all the items in sequence then the final result value will be returned.

Let’s use this to convert list to string,

list_of_words = ["This", "is", "a", "sample", "program"]

delimiter = ' '
final_str = functools.reduce(lambda a,b : a + delimiter + b, list_of_words)

print(final_str)

Output:

This is a sample program

Here we passed two arguments to the reduce() function,

  • A lambda function that accepts 2 arguments and join those arguments with a delimiter in between.
  • A list of strings

It joined all the elements in the list to create a string, using logic provided by lambda function.

Convert list of integers to string using reduce() in python

list_of_num = [1, 2, 3, 4, 5]

delimiter = ' '
final_str = functools.reduce(lambda a, b: str(a) + delimiter + str(b), list_of_num)

print(final_str)

Output:

1 2 3 4 5

Here we passed two arguments to the reduce() function,

  • A lambda function that accepts 2 arguments. Then converts both the arguments to string and join those arguments with a delimiter in between.
  • A list of strings

It joined all the integers in the list to create a string, using logic provided by lambda function.

Convert list to string using map() in python

map() function in python accepts 2 arguments i.e. a function and an iterable sequence. Then calls the given function on each element in the sequence and returns an Iterator to iterate over the result objects.

We can use that to convert list to string i.e.

mix_list = ["This", "is", "a", "sample", 44, 55, 66, "program"]

delimiter = ' '

# Convert list of items to a string value
final_str = delimiter.join(map(str, mix_list))
print(final_str)

Output:

This is a sample 44 55 66 program

Here we passed two arguments to the map() function,

  • str() function, that converts the given object to string value
    A list of different type of items.
  • It iterated over all the values in list and called the str() function on each item. All string values were returned through an Iterator. Then using join() function,

We joined all the string values returned by Iterator using join() to generate a concatenated string.

The complete example is as follows,

import functools


def convert_list_to_string(org_list, seperator=' '):
    """ Convert list to string, by joining all item in list with given separator.
        Returns the concatenated string """
    return seperator.join(org_list)


def main():
    print('*** Convert list to string using join() in python ***')

    print('*** Convert list to string with space as delimiter***')

    list_of_words = ["This", "is", "a", "sample", "program"]

    # Convert list of strings to string
    full_str = convert_list_to_string(list_of_words)

    print(full_str)

    print('*** Convert list to string with comma as delimiter***')

    # Join all the strings in list
    full_str = convert_list_to_string(list_of_words, ',')

    print('*** Convert list to string with custom delimiter ***')

    # Join all the strings in list
    full_str = convert_list_to_string(list_of_words, '---')

    print(full_str)

    print('*** Convert list to int to string ***')

    list_of_num = [1, 2, 3, 4, 5]

    # Covert list of integers to a string
    full_str = ' '.join([str(elem) for elem in list_of_num])

    print(full_str)

    print('*** Convert list of different type items to string ***')

    mix_list = ["This", "is", "a", "sample", 44, 55, 66, "program"]

    # Covert list of different type of items to string
    full_str = ' '.join([str(elem) for elem in mix_list])

    print(full_str)

    print('*** Convert list to string using reduce() ***')

    list_of_words = ["This", "is", "a", "sample", "program"]

    delimiter = ' '
    final_str = functools.reduce(lambda a,b : a + delimiter + b, list_of_words)

    print(final_str)

    print('*** Convert list of ints to string using reduce() ***')

    list_of_num = [1, 2, 3, 4, 5]

    delimiter = ' '
    final_str = functools.reduce(lambda a, b: str(a) + delimiter + str(b), list_of_num)

    print(final_str)

    print('*** Convert list to string using map() and join() ***')

    mix_list = ["This", "is", "a", "sample", 44, 55, 66, "program"]

    delimiter = ' '

    # Convert list of items to a string value
    final_str = delimiter.join(map(str, mix_list))
    print(final_str)

if __name__ == '__main__':
   main()

Output:

*** Convert list to string using join() in python ***
*** Convert list to string with space as delimiter***
This is a sample program
*** Convert list to string with comma as delimiter***
*** Convert list to string with custom delimiter ***
This---is---a---sample---program
*** Convert list to int to string ***
1 2 3 4 5
*** Convert list of different type items to string ***
This is a sample 44 55 66 program
*** Convert list to string using reduce() ***
This is a sample program
*** Convert list of ints to string using reduce() ***
1 2 3 4 5
*** Convert list to string using map() and join() ***
This is a sample 44 55 66 program

1 thought on “Convert list to string in python using join() / reduce() / map()”

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top