How to Print Lists Without Brackets and Commas in Python
By default, printing a list in Python displays it with square brackets and commas separating the elements.
This guide explains how to print a list's contents without these delimiters, producing cleaner, more customized output. We'll cover using str.join()
, the unpacking operator (*
), and how to handle lists containing non-string elements.
Using str.join()
(Recommended for Custom Separators)
The str.join()
method is the most flexible and Pythonic way to print a list's contents without brackets and with a customizable separator.
Joining Lists of Strings
If your list contains strings, you can directly use join()
:
list_of_strings = ['tutorial', 'reference', '.com']
result = ''.join(list_of_strings) # Join with NO separator
print(result) # Output: tutorialreference.com
result = ' '.join(list_of_strings) # Join with space
print(result) # Output: tutorial reference .com
result = '\n'.join(list_of_strings) # Join with newline character.
print(result)
Output:
tutorial
reference
.com
''.join(list_of_strings)
: Joins the elements with no separator (empty string).' '.join(list_of_strings)
: Joins the elements with a space separator.'\n'.join(list_of_strings)
: Joins the elements with a newline separator.- You can use any string as the separator.
Joining Lists of Numbers
str.join()
requires a list of strings. If your list contains numbers (integers, floats), you need to convert them to strings first. A generator expression is the most efficient way to do this:
list_of_integers = [7, 21, 44]
result = ''.join(str(item) for item in list_of_integers)
print(result) # Output: 72144
result = ' '.join(str(item) for item in list_of_integers)
print(result) # Output: 7 21 44
result = '\n'.join(str(item) for item in list_of_integers)
print(result)
Output:
7
21
44
(str(item) for item in list_of_integers)
: This is a generator expression. It efficiently converts each number to a string as it's needed byjoin()
, without creating an intermediate list. This is generally more memory-efficient than a list comprehension, especially for large lists.- Alternatively, you can use the
map
function:
list_of_integers = [7, 21, 44]
result = ''.join(map(str, list_of_integers))
print(result) # Output: 72144
Using the Unpacking Operator (*
) with print()
The unpacking operator (*
) provides a very concise way to print list elements without brackets and with a specified separator, all within the print()
function itself:
list_of_strings = ['tutorial', 'reference', '.com']
print(*list_of_strings, sep='') # Output: tutorialreference.com
print(*list_of_strings, sep=' ') # Output: tutorial reference .com
print(*list_of_strings, sep='\n') # Output: Each item on new line.
list_of_integers = [7, 21, 44]
print(*list_of_integers, sep='') # Output: 72144 (no separator)
*list_of_strings
: The asterisk unpacks the list, passing each element as a separate argument toprint()
.sep=''
: Thesep
keyword argument toprint()
controls the separator between the arguments. Setting it to''
(empty string) removes any separation. The default is a space.- No string conversion needed:
print()
automatically handles converting numbers to strings, so you don't needstr()
ormap()
with this approach. This is a major advantage.
The unpacking operator with print()
is usually the simplest option if you want space-separated output or no separator. If you need a different separator (like a comma, hyphen, or newline), str.join()
is more flexible.