How to Convert a Python List to String?

How to Convert a Python List to String?

6 mins read896 Views Comment
clickHere
Updated on Mar 2, 2023 11:29 IST

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!

2022_06_list-to-string-1.jpg

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:

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:

commands

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.

diagram

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

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:

strings2
example1
example2

From the above examples, we note that the spaces are counted as characters too.

Methods to Convert a Python List to String

convert

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 <pre class="python" style="font-family:monospace">\n \n <span style="color: #808080;font-style: italic">\n \n #Define a function \n \n
\n \n <span style="color: #ff7700;font-weight:bold">\n \n def toString\n \n <span style="color: black">\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 </span style="color: black">\n \n </span style="color: #ff7700;font-weight:bold">\n \n </span style="color: #808080;font-style: italic">\n \n </pre class="python" style="font-family:monospace">
Copy code

Output:

output1

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 <pre class="python" style="font-family:monospace">\n \n <span style="color: #808080;font-style: italic">\n \n #Define a function \n \n
\n \n <span style="color: #ff7700;font-weight:bold">\n \n def toString\n \n <span style="color: black">\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 </span style="color: black">\n \n </span style="color: #ff7700;font-weight:bold">\n \n </span style="color: #808080;font-style: italic">\n \n </pre class="python" style="font-family:monospace">
Copy code

Output:

output2

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.
Logging in Python: How to Implement a Robust Logging System for Your Applications
Logging in Python: How to Implement a Robust Logging System for Your Applications
Learn about logging in Python, a crucial tool for software development. Understand the built-in logging module, logging levels, configuring logging, and best practices for writing effective and efficient logs. Implement...read more
Python Pass Statement: A Simple Solution for Placeholder Code Blocks
Python Pass Statement: A Simple Solution for Placeholder Code Blocks
The “pass” statement is a placeholder in Python code that signifies that a particular code block is empty or yet to be written. In this article, we will learn how...read more
Python Yield(): A Comprehensive Guide to Understanding and Implementing the Keyword
Python Yield(): A Comprehensive Guide to Understanding and Implementing the Keyword
In this article, we will learn what yield keyword in python is, how to use yield to create generators. Later in the article, we will also discuss best practices and...read more

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 <pre class="python" style="font-family:monospace">\n \n mylist \n \n <span style="color: #66cc66">\n \n = \n \n <span style="color: black">\n \n [\n \n <span style="color: #483d8b">\n \n 'I'\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 </span style="color: #483d8b">\n \n </span style="color: black">\n \n </span style="color: #66cc66">\n \n </pre class="python" style="font-family:monospace">
Copy code

Output:

output3

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 <pre class="python" style="font-family:monospace">\n \n mylist \n \n <span style="color: #66cc66">\n \n = \n \n <span style="color: black">\n \n [\n \n <span style="color: #483d8b">\n \n "Method"\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 </span style="color: #483d8b">\n \n </span style="color: black">\n \n </span style="color: #66cc66">\n \n </pre class="python" style="font-family:monospace">
Copy code

Output:

output4

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.

About the Author

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