How to convert list to string
Categories:
How to Convert a List to a String in Python

Learn various effective methods to convert a list of items into a single string in Python, covering common use cases and best practices.
Converting a list to a string is a common operation in Python programming, especially when you need to display list contents, write them to a file, or process them as a single text block. Python offers several straightforward and efficient ways to achieve this, each suitable for different scenarios. This article will explore the most popular methods, including the join() method, for loops, and map() function, providing practical examples for each.
Using the join() Method
The str.join() method is the most Pythonic and efficient way to concatenate elements of an iterable (like a list) into a string. It takes an iterable as an argument and uses the string on which it's called as a delimiter between the elements. This method is highly optimized and generally preferred for performance.
my_list = ['apple', 'banana', 'cherry']
separator = ', '
my_string = separator.join(my_list)
print(my_string)
# Example with no separator
words = ['Hello', 'World']
no_separator_string = ''.join(words)
print(no_separator_string)
# Example with numbers (requires conversion)
numbers = [1, 2, 3]
string_numbers = [str(num) for num in numbers]
print(''.join(string_numbers))
Basic usage of str.join() with different separators and handling numbers.
join() method is significantly faster than using a for loop with string concatenation (+=) because it avoids creating multiple intermediate string objects.
Workflow for using the join() method.
Converting Lists with Non-String Elements
The join() method exclusively works with iterables containing string elements. If your list contains numbers or other non-string types, you'll need to convert them to strings before using join(). This can be efficiently done using a list comprehension or the map() function.
# Using list comprehension
num_list = [10, 20, 30]
stringified_list = [str(item) for item in num_list]
result_string_comp = '-'.join(stringified_list)
print(f"List comprehension result: {result_string_comp}")
# Using map()
obj_list = [1, 'hello', 3.14, True]
mapped_to_string = map(str, obj_list)
result_string_map = ' '.join(mapped_to_string)
print(f"Map function result: {result_string_map}")
Converting mixed-type lists to strings using list comprehensions and map().
Alternative: Using a for Loop (Less Efficient for Large Lists)
While join() is generally preferred, a for loop can also be used, especially if you need more complex logic during concatenation or if dealing with very small lists where performance differences are negligible. However, repeated string concatenation using + or += inside a loop can be inefficient due to the immutability of strings in Python, leading to the creation of new string objects in memory with each iteration.
my_list = ['alpha', 'beta', 'gamma']
result_string = ""
for item in my_list:
result_string += item + " " # Adds a space after each item
# Remove trailing space if necessary
result_string = result_string.strip()
print(result_string)
# A slightly better approach for loops (pre-allocating a list of strings)
parts = []
for item in my_list:
parts.append(str(item))
result_string_better_loop = ', '.join(parts)
print(result_string_better_loop)
Converting a list to a string using a for loop and string concatenation.
+ or += for string concatenation in a loop with a large number of iterations, as it can be very inefficient. For such cases, join() is the superior choice.