In this tutorial, we’ll look at how to convert a python list to a string. Show
How to convert a python list to a string?You can use the string join() function in a list comprehension to easily create a string from the elements of the list. The following is the syntax: s = ''.join(str(i) for i in ls)Here, we iterate through the elements of the list ls converting each to a string object using list comprehension and then join the elements into a string using the string join() method. For more details on the join() method, refer to this guide. ExamplesLet’s look at a few examples. 1. List of string to stringTo get a single string from a list of strings, you can use the syntax above. But, since all the items in the list are already string, you don’t need to explicitly convert them into a string. See the example below. ls = ['a', 'e', 'i', 'o', 'u'] s = ''.join(ls) print(s)Output: aeiouIn the above example, you can see that we directly passed the list to the join() function without converting each item to string. This was possible because the items in the list were already string. 2. List of integers to stringTo get a string from a list of integers, you need to explicitly convert each list item to a string (for example, in a list comprehension) before applying the join() function. See the example below. ls = [1, 2, 3, 4] s = ''.join(str(i) for i in ls) print(s) print(type(s))Output: 1234 <class 'str'>In the above example, the string s is created from the items of the list ls containing integer items. If you’re not sure about the types of items in the list, it’s better to convert them to string and then pass it to the join() function which is used to join string objects. 3. List to string with each item on newlineYou can use a custom string to join the items in the list. The above examples used an empty string to join the items but you use any other string like a whitespace ' ', newline character '\n', etc as well. See the example below. ls = [1, 2, 3, 4] s = '\n'.join(str(i) for i in ls) print(s)Output: 1 2 3 4Here, we use a newline character '\n' to join the list. The resulting string has each item of the list separated by a newline character. For more on the python string join() function, refer to the python docs.
In this article we will discuss different ways to convert list to string. Convert list to string in python using join() in pythonIn 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 pythonSuppose 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 programAs 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 pythondef 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 programIn 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 pythonWe 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---programIn 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 pythonSuppose 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 5We 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 5We joined all the elements by using a white space as delimiter. Convert list of different type items to string using join() in pythonSuppose 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 programWe 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 programWe joined all the elements by using a white space as delimiter. Convert list to string using reduce() in pythonreduce(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 programHere we passed two arguments to the reduce() function,
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 pythonlist_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 5Here we passed two arguments to the reduce() function,
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 pythonmap() 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 programHere we passed two arguments to the map() 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 |