You are currently viewing Python Join List to String by Delimiter

Let’s see how to convert List to String by a delimiter in python using join() method, this method can take parameters either list/string/set e.t.c.

Advertisements

join() is used to join the given variable like string/list into a string. It will join with a separator which we need to pass along with the join() method. It will take one input parameter list/string that has to be joined.

1. Quick examples of Joining List to a String by Delimiter

Following are quick examples of joining a list to a string by a delimiter.


# Consider list of countries
countries=['China','India','Japan','UK','USA']

# Using join()
print(", ".join(countries))

# Using join() with List Comprehension
print(", ".join([i for i in countries]))

# Using join() with for loop
print(", ".join(i for i in countries))

# Using join() with map()
print(", ".join(map(str, countries)))

2. Join List to String in Python by Delimiter using join()

The join() is used to join the given variable like string/list to a string by separating each element by a delimiter/separator, it will join with a separator which we need to pass along with the join() method. It will take one input parameter lime list/string that has to be joined.

2.1 join() Syntax

Let’s see how to use join() method to convert list to string


# Here, mylist is the input list.
", ".join(mylist)

3. Join List to String Examples

3.1 Example 1:

Let’s pass List to the join() method.


# Consider list of countries
countries=['China','India','Japan','UK','USA']

# Using join()
print(", ".join(countries))

# Output:
# China, India, Japan, UK, USA

Here, there are 5 elements in the list. After applying join(), all the elements in the list are converted to string.

Example 2:

Let’s pass List Comprehension to the join() method.


# Consider list of countries
countries=['China','India','Japan','UK','USA']

# Using join() with List Comprehension
print(", ".join([i for i in countries]))

# Output:
# China, India, Japan, UK, USA

For loop is used to iterate all elements in the list and we are passing this in the List comprehension. This List comprehension is passed as a parameter to the join() method.

Here, there are 5 elements in the list. After applying join(), all the elements in the list are converted to string.

Example 3:

Let’s pass For loop to the join() method by passing for loop as a parameter.


# Consider list of countries
countries=['China','India','Japan','UK','USA']

# Using join() with for loop
print(", ".join(i for i in countries))

# Output:
# China, India, Japan, UK, USA

For loop is used to iterate all elements in the list and we are passing this as parameter to the join() method.

Here, there are 5 elements in the list. After applying join(), all the elements in the list are converted to string.

Example 4:

Let’s pass the map() function to the join() method by passing for loop as a parameter.

map() is used to map the given data type to another. It will take first parameter as a data type to be converted and second parameter is the list.

If we pass this to the join(), list is converted to string.


# Consider list of countries
countries=['China','India','Japan','UK','USA']

# Using join() with map()
print(", ".join(map(str, countries)))

# Output:
# China, India, Japan, UK, USA

3. Conclusion

In this article, you have learned how to convert the given list to a string in Python using the join() method, and each element of the list is separated by a delimiter in a string.