How to Convert a Python List to String?
In Python, converting a list to a string is a common operation in order to make it convenient to perform data manipulation. In this article, we will briefly discuss the two data types and guide you through 4 popular methods to perform conversion from a list to a string. So, without further ado, let’s get started!
The below articles go through the ways to convert a python list to string. Conversion is a common operation to perform data manipulation. We will be covering the following sections today:
Explore: Python Courses
What is a Python List?
A list is a collection of variables of various data types, such as integers, floats, strings, objects, or even other lists. Lists are mutable in nature, meaning they can be altered post-creation.
In Python, they are used to store heterogeneous data by simply separating the list elements using commas and putting them in square brackets. Let’s look at a few examples:
Python lists are ordered, which means that every element in the list is indexed. The index can start from the first element for the last element. For example, in list1 shown above, if we start ordering from the first element, its index = 0. However, if we order the list starting from the last element, its index = -1.
Best-suited Python courses for you
Learn Python with these high-rated online courses
What is a Python String?
Now, let’s talk about a Python string which is an immutable data type that cannot be altered once declared in a program. It is denoted by a sequence of Unicode characters or symbols enclosed in a single or double quote. Let’s look at the examples:
Strings in Python are indexed as well. Let’s try to access the element at index 4 of string1, and the element at index -5 of string2. The output is displayed below:
From the above examples, we note that the spaces are counted as characters too.
Methods to Convert a Python List to String
Method 1 – Traversing over the list
In this method, we use the for loop to iterate every index of the input list and add it to an empty string one by one. Let’s look at an example:
\n \n \n <pre class="python" style="font-family:monospace">\n \n \n <span style="color: #808080;font-style: italic">\n \n \n #Define a function \n \n \n \n \n \n <span style="color: #ff7700;font-weight:bold">\n \n \n def toString\n \n \n <span style="color: black">\n \n \n (\n \n \n <span style="color: #008000">\n list \n <span style="color: black">\n ): \n \n <span style="color: #808080;font-style: italic">\n #Initialize an empty string \n \n <span style="color: #008000">\n str \n <span style="color: #66cc66">\n = \n <span style="color: #483d8b">\n '' \n \n \n <span style="color: #808080;font-style: italic">\n #Traverse the list \n \n <span style="color: #ff7700;font-weight:bold">\n for i \n <span style="color: #ff7700;font-weight:bold">\n in \n <span style="color: #008000">\n list: \n \n <span style="color: #008000">\n str + \n <span style="color: #66cc66">\n = i \n \n \n <span style="color: #808080;font-style: italic">\n #Return string \n \n <span style="color: #ff7700;font-weight:bold">\n return \n <span style="color: #008000">\n str \n \n \n <span style="color: #808080;font-style: italic">\n #Passing string data type to list \n mylist \n <span style="color: #66cc66">\n = \n <span style="color: black">\n [ \n <span style="color: #483d8b">\n "Let's" \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n " learn" \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n " Python!" \n <span style="color: black">\n ] \n \n \n <span style="color: #808080;font-style: italic">\n #Convert list to string \n \n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n (toString \n <span style="color: black">\n (mylist \n <span style="color: black">\n ) \n <span style="color: black">\n ) \n \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #808080;font-style: italic"> \n </span style="color: black"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #66cc66"> \n </span style="color: #808080;font-style: italic"> \n </span style="color: #008000"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #808080;font-style: italic"> \n </span style="color: #66cc66"> \n </span style="color: #008000"> \n </span style="color: #008000"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #808080;font-style: italic"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #008000"> \n </span style="color: #808080;font-style: italic"> \n </span style="color: black"> \n </span style="color: #008000">\n \n \n </span style="color: black">\n \n \n </span style="color: #ff7700;font-weight:bold">\n \n \n </span style="color: #808080;font-style: italic">\n \n \n </pre class="python" style="font-family:monospace">
Output:
What have we done here?
- We define a function toString() that accepts a list variable as an argument.
- Inside the function body, we initialize an empty string variable str.
- Then, we traverse every element of the list from start to end using the for loop and add it to the empty string.
- Once the loop has completed its execution, the function returns the string.
- Outside the function body, we pass elements of string data type to the mylist variable.
- Lastly, we print the converted string by calling the function.
Method 2 – Using join function
The Python join() method takes iterable objects such as lists, tuples, sets, etc., and returns them as a string. However, note that the elements in these objects should be of string data type.
Let’s look at a particular example of converting a list to a string:
\n \n \n <pre class="python" style="font-family:monospace">\n \n \n <span style="color: #808080;font-style: italic">\n \n \n #Define a function \n \n \n \n \n \n <span style="color: #ff7700;font-weight:bold">\n \n \n def toString\n \n \n <span style="color: black">\n \n \n (\n \n \n <span style="color: #008000">\n list \n <span style="color: black">\n ): \n \n <span style="color: #808080;font-style: italic">\n #Initialize an empty string \n \n <span style="color: #008000">\n str \n <span style="color: #66cc66">\n = \n <span style="color: #483d8b">\n '' \n \n \n <span style="color: #808080;font-style: italic">\n #Return string \n \n <span style="color: #ff7700;font-weight:bold">\n return \n <span style="color: black">\n ( \n <span style="color: #008000">\n str. \n <span style="color: black">\n join \n <span style="color: black">\n ( \n <span style="color: #008000">\n list \n <span style="color: black">\n ) \n <span style="color: black">\n ) \n \n \n <span style="color: #808080;font-style: italic">\n #Passing string data type to list \n mylist \n <span style="color: #66cc66">\n = \n <span style="color: black">\n [ \n <span style="color: #483d8b">\n "Hello" \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n " world!" \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n " Python" \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n " is" \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n " fun" \n <span style="color: black">\n ] \n \n \n <span style="color: #808080;font-style: italic">\n #Convert list to string \n \n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n (toString \n <span style="color: black">\n (mylist \n <span style="color: black">\n ) \n <span style="color: black">\n ) \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #808080;font-style: italic"> \n </span style="color: black"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #66cc66"> \n </span style="color: #808080;font-style: italic"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #008000"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #008000"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #808080;font-style: italic"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #008000"> \n </span style="color: #808080;font-style: italic"> \n </span style="color: black"> \n </span style="color: #008000">\n \n \n </span style="color: black">\n \n \n </span style="color: #ff7700;font-weight:bold">\n \n \n </span style="color: #808080;font-style: italic">\n \n \n </pre class="python" style="font-family:monospace">
Output:
What have we done here?
- Firstly, we define a function toString() that accepts a list variable as an argument.
- Inside the function body, we initialize an empty string variable str.
- Then, we use the join() function to convert the list to a string by concatenating the elements of the list into a string and adding to the string variable str.
- Once all elements have been converted, the function returns the string.
- Outside the function body, we pass elements of string data type to the mylist variable.
- Lastly, we print the converted string by calling the function.
Method 3 – Using map function
The Python map() method loops every item in an input iterable object (a list, tuple, set, etc.), and maps its conversion to string using an input function.
So, this method takes two arguments – the function to be applied on the items of the iterable, and the iterable object. Let’s understand through an example:
\n \n \n <pre class="python" style="font-family:monospace">\n \n \n mylist \n \n \n <span style="color: #66cc66">\n \n \n = \n \n \n <span style="color: black">\n \n \n [\n \n \n <span style="color: #483d8b">\n \n \n 'I'\n \n \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'want' \n <span style="color: #66cc66">\n , \n <span style="color: #ff4500">\n 2 \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'learn' \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n 'Python' \n <span style="color: black">\n ] \n \n \n <span style="color: #808080;font-style: italic">\n #Using map function \n \n <span style="color: #ff7700;font-weight:bold">\n def toString \n <span style="color: black">\n ( \n <span style="color: #008000">\n list \n <span style="color: black">\n ) - \n <span style="color: #66cc66">\n > \n <span style="color: #008000">\n str: \n \n <span style="color: #ff7700;font-weight:bold">\n return \n <span style="color: #483d8b">\n " ". \n <span style="color: black">\n join \n <span style="color: black">\n ( \n <span style="color: #008000">\n map \n <span style="color: black">\n ( \n <span style="color: #008000">\n str \n <span style="color: #66cc66">\n , \n <span style="color: #008000">\n list \n <span style="color: black">\n ) \n <span style="color: black">\n ) \n \n \n <span style="color: #808080;font-style: italic">\n #Convert list to string \n \n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n (toString \n <span style="color: black">\n (mylist \n <span style="color: black">\n ) \n <span style="color: black">\n ) \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #808080;font-style: italic"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #008000"> \n </span style="color: #66cc66"> \n </span style="color: #008000"> \n </span style="color: black"> \n </span style="color: #008000"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #483d8b"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #008000"> \n </span style="color: #66cc66"> \n </span style="color: black"> \n </span style="color: #008000"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #808080;font-style: italic"> \n </span style="color: black"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66">\n \n \n </span style="color: #483d8b">\n \n \n </span style="color: black">\n \n \n </span style="color: #66cc66">\n \n \n </pre class="python" style="font-family:monospace">
Output:
What have we done here?
- We first pass elements of different data types to the mylist variable.
- Then, we define a function toString() that accepts a list variable as an argument and converts it to a Python string str.
- Next, we will simply return our string whose elements have been converted using the map() function and concatenated by applying a join() function using a space separator.
- Lastly, we print the converted string by calling the toString() function.
Method 4 – Using list comprehension
In this method, what we essentially do is convert every element in a given list into a string and join them all together using the join() function. Here, unlike Method 2, the elements of the list need not be of string data type only.
This is a simple method that works on a single line of code. Let’s understand through an example:
\n \n \n <pre class="python" style="font-family:monospace">\n \n \n mylist \n \n \n <span style="color: #66cc66">\n \n \n = \n \n \n <span style="color: black">\n \n \n [\n \n \n <span style="color: #483d8b">\n \n \n "Method"\n \n \n <span style="color: #66cc66">\n , \n <span style="color: #ff4500">\n 4 \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n "is" \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n "List" \n <span style="color: #66cc66">\n , \n <span style="color: #483d8b">\n "Comprehension" \n <span style="color: black">\n ] \n \n \n <span style="color: #808080;font-style: italic">\n #Convert list to string \n \n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n ( \n <span style="color: #483d8b">\n " ". \n <span style="color: black">\n join \n <span style="color: black">\n ( \n <span style="color: black">\n [ \n <span style="color: #008000">\n str \n <span style="color: black">\n (i \n <span style="color: black">\n ) \n <span style="color: #ff7700;font-weight:bold">\n for i \n <span style="color: #ff7700;font-weight:bold">\n in mylist \n <span style="color: black">\n ] \n <span style="color: black">\n ) \n <span style="color: black">\n ) \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #008000"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: black"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #808080;font-style: italic"> \n </span style="color: black"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #483d8b"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66">\n \n \n </span style="color: #483d8b">\n \n \n </span style="color: black">\n \n \n </span style="color: #66cc66">\n \n \n </pre class="python" style="font-family:monospace">
Output:
What have we done here?
- We first pass elements of different data types to the mylist variable.
- Then, we simply print the converted string after:
- traversing each element of the list to convert to string, and
- concatenating them by applying the join() function using a space separator.
Endnotes
Hope this article was helpful for you to understand the list and string data types and learn how to convert a list to a string using different methods. Converting lists to strings in Python is a common practice and hence, the most efficient way would be to define a function that performs the conversion using any of the aforementioned methods. Want to learn more about Python and practice Python programming? Explore related articles here.
Top Trending Tech Articles:Career Opportunities after BTech Online Python Compiler What is Coding Queue Data Structure Top Programming Language Trending DevOps Tools Highest Paid IT Jobs Most In Demand IT Skills Networking Interview Questions Features of Java Basic Linux Commands Amazon Interview Questions
______________
Recently completed any professional course/certification from the market? Tell us what liked or disliked in the course for more curated content.
Click here to submit its review with Shiksha Online.
This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio