{"id":2863,"date":"2023-10-17T09:24:35","date_gmt":"2023-10-17T03:54:35","guid":{"rendered":"https:\/\/python-programs.com\/?p=2863"},"modified":"2023-11-10T11:45:11","modified_gmt":"2023-11-10T06:15:11","slug":"python-convert-list-to-string","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-convert-list-to-string\/","title":{"rendered":"Convert List to String in Python using join() \/ reduce() \/ map()"},"content":{"rendered":"
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.<\/p>\n
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.<\/p>\n
This article will go over various methods to convert list to string.<\/p>\n
Examples:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n There are several ways to convert list to string some of them are:<\/p>\n The elements of the input list are iterated one by one and added to a new empty string in this process.<\/p>\n As a result, a list is converted to a string.<\/p>\n Below is the implementation:<\/p>\n Output:<\/strong><\/p>\n The join() method in Python can be used to convert a List to a String.<\/p>\n Iterables such as Lists, Tuples, Strings, and others are accepted as parameters for the join() process.<\/p>\n It also returns a new string as an argument that contains the elements concatenated from the iterable.<\/p>\n The passed iterable must contain string elements as a prerequisite for the join() method to work.<\/p>\n A TypeError exception is thrown if the iterable includes an integer.<\/p>\n Output:<\/strong><\/p>\n The map() function in Python can be used to convert a list to a string.<\/p>\n 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.<\/p>\n General syntax of map:<\/p>\n Output:<\/strong><\/p>\n The map(str, givenlist) function in the preceding code snippet accepts str function and given list as arguments.<\/p>\n It maps each element of the input iterable( list) to the given function and returns the resultant list of elements.<\/p>\n In addition, the join() method is used to convert the output to string form.<\/p>\n Note: This method also applicable to list of integers since every element is converted to list.<\/p>\n 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.<\/p>\n 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.<\/p>\n Below is the implementation:<\/p>\n Output:<\/strong><\/p>\n We passed two arguments to the reduce() function in this case,<\/p>\n Using the logic provided by the lambda function, it joined all of the elements in the list to form a string. 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 …<\/p>\ngivenlist=[\"this\" , \"is\" , \"Btech\" ,\"Geeks\"]<\/pre>\n
this is btech Geeks<\/pre>\n
List to String conversion<\/h2>\n
\n
Method #1 : Using String Concatenation<\/h3>\n
# Function which converts list to string\r\ndef listToString(givenlist):\r\n # Taking eempty string say string\r\n string = \"\"\r\n # Traverse the list and concate the elements to the string\r\n for element in givenlist:\r\n # converting element of list to string and concating to empty string\r\n # concatenating space to differentiate words(deliminator)\r\n string = string + str(element) + \" \"\r\n\r\n # return the final string\r\n return string\r\n\r\n\r\n# Driver code\r\n# given list\r\ngivenlist = [\"hello\", \"this\", \"is\", \"Btech\", \"Geeks\"]\r\n\r\n# passing list to convert into string\r\nprint(listToString(givenlist))\r\n<\/pre>\n
hello this is Btech Geeks<\/pre>\n
Method #2:Using join()<\/h3>\n
# Function which converts list to string\r\ndef listToString(givenlist):\r\n # using space \" \" as separator between the elements of list\r\n string = ' '.join(givenlist)\r\n\r\n # return the final string\r\n return string\r\n\r\n\r\n# Driver code\r\n# given list\r\ngivenlist = [\"hello\", \"this\", \"is\", \"Btech\", \"Geeks\"]\r\n\r\n\r\n# passing list to convert into string\r\nprint(listToString(givenlist))\r\n<\/pre>\n
hello this is Btech Geeks<\/pre>\n
Method #3:Using map()<\/h3>\n
map(function, iterable)<\/code><\/p>\n
# Function which converts list to string\r\ndef listToString(givenlist):\r\n # using space \" \" as separator between the elements of list\r\n string = ' '.join(map(str, givenlist))\r\n\r\n # return the final string\r\n return string\r\n\r\n\r\n# Driver code\r\n# given list\r\ngivenlist = [\"hello\", \"this\", \"is\", \"Btech\", \"Geeks\"]\r\n\r\n\r\n# passing list to convert into string\r\nprint(listToString(givenlist))\r\n<\/pre>\n
hello this is Btech Geeks<\/pre>\n
Method #4:Using reduce()<\/h3>\n
import functools\r\n# Function which converts list to string\r\n\r\n\r\ndef listToString(givenlist):\r\n # deliminator or separator between the elements of list let us say it as space\r\n deliminator = \" \"\r\n \r\n # using reduce function\r\n string = functools.reduce(lambda a, b: a + deliminator + b, givenlist)\r\n\r\n # return the final string\r\n return string\r\n\r\n\r\n# Driver code\r\n# given list\r\ngivenlist = [\"hello\", \"this\", \"is\", \"Btech\", \"Geeks\"]\r\n\r\n\r\n# passing list to convert into string\r\nprint(listToString(givenlist))\r\n<\/pre>\n
hello this is Btech Geeks<\/pre>\n
\n
\nRelated Programs<\/strong>:<\/p>\n\n