In this article, we will see the Python list to string conversion with coding examples. For this, first, we have to know what is a list and what is a string?
A list in Python serves the purpose of holding elements for manipulation. It represents a collection of homogeneous elements. If the programmer is familiar with the C programming language, then he might know about arrays.
Lists are very similar to arrays; only one difference is that they can contain any data type, unlike arrays. A list is a collection of ordered objects. Each element’s index is according to a sequence in the list, and the index starts from 0.
The list is a ‘Collection Datatype’ containing integers, strings, or other lists. Lists are mutable; hence, we can change them even after their creation. Also, note that lists can contain duplicate entries, unlike sets.
The String in Python also serves the purpose of holding elements in the form of characters. So, what exactly are strings? A string is one of Python’s data types, and we can define it as a sequence of characters.
There is a built-in class str for handling a Python string. Creating a string is easy, like assigning a value to a variable.
We can do list to string conversion by following methods:
- Using join() method
- Using list comprehension
- Iterating using for loop
- Using map() method
Table of Contents
Approach#01: List to String Conversion Using join() Method
We can use the join() method in Python to convert a list into a string. The join() method takes iterables (such as list, tuples, strings, etc.).
Further, it returns the elements from the iterables and concatenates them for the output. For the join() method to work properly, the data passing to iterables must be in String data type. If we give any integer or other type inside the iterables, it will throw an exception error as TypeError Exception.
Syntax for the join() method
Let’s have a look at an example that contains a list of string.
l_list = ["Code", "Leaks", "Example"] s_string = " " c_string = s_string.join(l_list) print(c_string)
Approach#02: List to String Conversion Using List Comprehension
List Comprehension in Python creates a list of elements from an existing list. It further uses the for loop to traverse the items of the iterable in an element-wise pattern.
Python List Comprehension, along with the join() method, can convert a list to a string. The list comprehension will traverse the elements element-wise, and the join() method will concatenate the elements of the list to a new string and represent it as output.
Let’s have a look at an example.
l_list = ["Code", "Leaks", "Example"] string = " " com = string.join([str(item) for item in l_list]) print(com)
Approach#03: List to String Conversion Using for Loop Iteration
l_list = ["Code", "Leaks", "Example"] string = "" for x in l_list: string += " " + x print(string)
Approach#04: List to String Conversion Using map() Method
We can use Python’s map() function to convert a list to a string.
The map() function accepts two arguments, i.e., the function and iterable objects such as lists, tuples, strings, etc. Further, the map() function maps the elements of the iterable with the provided function.
In the following example, the map() method will take the elements from l_list, an iterable list, and map it to the str, a String. Finally, it is given to the join() Method, which will convert the resulting output of the map() Method into a string.
Syntax for the map() method
Let’s have a look at an example.
l_list = ["Code", "Leaks", "Example"] string = " " com = string.join(map(str, l_list)) print(com)