How to Convert List to String in Python – Multiple Methods

Convert List To String In Python

Python offers various methods that facilitate converting a list to a string.

A list in Python is a mutable ordered collection that can store elements of different data types. You can modify their elements by adding, removing, or updating them. On the other hand, a string is an immutable sequence of characters representing a piece of text. You can perform various operations on strings, such as concatenation, slicing, etc.

In this tutorial, we will take a look at several ways to convert lists to strings in Python. 


Why Convert Python List to String?

There are many reasons to convert the elements in the list into a string. For example, printing or logging a string might be simpler than a list. By converting lists to strings, you gain the ability to perform string operations directly, which is not available when working with Python lists. Additionally, there may be instances where you need to transmit list data, and using a string format is often the most appropriate method.

At last, converting a list gives us the power to use all the functionality of a string on the list items. With that being said, let’s see what methods we will use for that conversion.

A list can be converted to a string by following methods:

  • By using join() method
  • By using List Comprehension
  • By using map() method
  • By using for loop

1. Convert List to String using join() Method

Python join() method can be used to convert the List to a String in Python. The join() method accepts iterables as a parameter, such as Lists, Tuples, etc. Further, it returns a single string that contains the elements concatenated from the iterable passed as an argument.

Note: The mandatory condition to use the join() method is that the passed iterable should contain string elements. If the iterable contains an integer, it raises a TypeError exception.

Syntax:

string.join(iterable)

where iterable can be a list need to convert into a string.

Example:

inp_list = ['John', 'Bran', 'Grammy', 'Norah'] 
out_str = " "
print("Converting list to string using join() method:\n")
print(out_str.join(inp_list)) 

In the above example, the join() method takes a list of strings inp_list as a parameter and concatenates its elements to a string out_str and prints it as output.

Output:

Converting list to string using join() method:

John Bran Grammy Norah

2. Convert List to String using List Comprehension along with the join() Method

Using the List Comprehension along with the join() method can be used to convert the list of any data type to a Python 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.

Syntax:

''.join([str(element) for element in iterable])

where iterable is the list you want to convert to a string.

Example:

inp_list = ['John', 'Bran', 'Grammy', 'Norah'] 

res = ' '.join([str(item) for item in inp_list]) 
print("Converting list to atring using List Comprehension:\n")
print(res) 

If you’re wondering where str() is declared, str() is a built-in Python function that can convert any object(including lists) to strings. It returns a string representation of the object passed as its argument.

Output:

Converting list to atring using List Comprehension:

John Bran Grammy Norah

3. Convert List to String using map() Function

Python’s map() function can be used to convert a list to a string. The map() function accepts a function and an iterable object such as a list, tuple, string, etc. Taking it ahead, the map() function maps the elements of the iterable with the function provided.

Syntax:

map(function, iterable)

where iterable is the list you want to convert to a string and the function can be a built-in function or a user-defined function that you want to apply to each element of the iterable.

Example:

inp_list = ['John', 'Bran', 'Grammy', 'Norah'] 

res = ' '.join(map(str, inp_list)) 
print("Converting list to string using map() method:\n")
print(res) 

In the above snippet of code, the map(str, inp_list) function accepts the str function and inp_list as arguments. It maps every element of the input iterable(i.e. list) to the given function and returns the list of elements. Further, the join() method is used to join & set the elements into a single string.

Output:

Converting list to string using map() method:

John Bran Grammy Norah

4. Convert List to String using for loop

In this technique, elements of the input list are iterated one by one and are added to a new empty string. Thus, converting a list to a string.

This is one of the most commonly used methods for this conversion as it only uses the for loop, no additional method is needed here.

Syntax:

for element in iterable:
    result += str(element)

where iterable is the list you want to convert to a string and the result is the output string.

Example:

inp_str = ['John', 'Bran', 'Grammy'] 
st = "" 	
for x in inp_str:
  st += x

print(st)

Output:

JohnBranGrammy

Convert a List of Characters into a String in Python

Even a set of characters in the form of a list can be converted to a string in the same manner as above.

Below is an example to demonstrate the conversion of a list of characters to a string.

Example:

inp_str = ['J', 'o', 'u', 'r', 'n', 'a', 'l', 'd', 'e', 'v']
st = ""
for x in inp_str: 
  st += x

print(st)

Output:

Journaldev

Conclusion

In this tutorial, we explored four different ways to convert a list to string in Python programming language. We learned about join(), map(), and list comprehension approaches and also used for loops with the += operator for conversion. Each method offers its own advantages and suitability for different scenarios. With this knowledge, you can efficiently convert a list to a string using Python.


Reference

https://stackoverflow.com/questions/5618878/how-to-convert-list-to-string