Print List Without Brackets in Python

Python, by default, prints a list with square brackets. This article discusses how we can format a list printed out in Python, precisely eliminating the square brackets enclosing the list elements.

There are so many ways to remove these brackets. Here are some methods (slightly different to fit various usage cases).

Method 1: Unpack the list and print

If we want to unpack the elements of a Python list, we preceded the list with an asterisk (*). For example

lst1 = ["Amsterdam", "Berlin", "Paris", "New York"]
print(*lst1)

Output:

Amsterdam Berlin Paris New York

That removes the square brackets. Here is what happened. When we unpack the list of elements, the individual elements are passed into the print function like in the following case.

print("Amsterdam", "Berlin", "Paris", "New York")

Output:

Amsterdam Berlin Paris New York

The print function has an argument called sep that determines how the printed elements are separated on the standard output (stdout). By default, sep=” “, that is, elements are separated by white space. You can issue another character to separate your elements. Examples below.

lst1 = ["Amsterdam", "Berlin", "Paris", "New York"]
print(*lst1, sep=",") # sep with comma
print(*lst1, sep="&") # separated with ampersand
print(*lst1, sep="\n") #separated with newline character

Output:

Amsterdam,Berlin,Paris,New York
Amsterdam&Berlin&Paris&New York
Amsterdam
Berlin
Paris
New York

Those lines prints elements separated by a comma, ampersand (&), and newline character (“\n”), respectively. Note that when elements are separated with a newline character (“\n”), each element is printed in a new line.

This method with all data types – not just strings.

lst2 = [1, "Berlin", 0.6, None]
print(*lst2, sep=",")

Output:

1,Berlin,0.6,None

For Python 2, you can access the sep argument in the print function by including the following import statement at the top of the script: from __future__ import print_function.

Method 2: String and string indexing

The key in this method is to convert the entire list into a string and then pick all the characters except the brackets by indexing. Unlike Method 1, this method will not remove the quotes on string data types.

lst1 = ["Amsterdam", "Berlin", "Paris", "New York"]
print(str(lst1)[1:-1])
lst2 = [1, "Berlin", 0.6, None]
print(str(lst2)[1:-1])

Output:

'Amsterdam', 'Berlin', 'Paris', 'New York'
1, 'Berlin', 0.6, None

In this method, we are not unpacking the list elements; therefore, we cannot issue the sep argument on the print statement because the whole list was converted to a string with the str function. <str>[1:-1] will slice <str> from the 2nd character to the second last character, effectively eliminating opening and closing square brackets at the first and the last positions, respectively.

This method is helpful if you are interested in eliminating brackets only.

Method 3: Using the join() function

The <sep>.join(iterable) is an in-built Python method used to join an iterable’s element, separated by a string separator (sep) that we must specify. All the elements in the iterable must be of string type. Here is an example,

lst1 = ["Amsterdam", "Berlin", "Paris", "New York"]
print(",".join(lst1))

Output:

Amsterdam,Berlin,Paris,New York

In this case, the list elements are joined using a comma(,). If you want each element printed in a new line, you can issue a newline (“\n”) separator as follows.

lst1 = ["Amsterdam", "Berlin", "Paris", "New York"]
print("\n".join(lst1))

Output:

Amsterdam
Berlin
Paris
New York

If you have a list with non-string data types, an attempt to join elements will lead to TypeError

lst2 = [1, "Berlin", 0.6, None]
# joining the elements of the list with non-string values
print("&".join(lst2))

Output:

TypeError: sequence item 0: expected str instance, int found

We can solve this problem by converting list elements into strings first before using the join() function, that is,

lst2 = [1, "Berlin", 0.6, None]
#convert elements of the list to string
lst3 = list(map(str, lst2))
print(lst3)
# joining the elements of the list with string values
print("&".join(lst3))

Output:

['1', 'Berlin', '0.6', 'None']
1&Berlin&0.6&None

The map(<function>, <iterable>) is a function that efficiently applies <function> to every element of the <iterable> and returns an iterator. In the code snippet above, list(map(str, lst2)) takes every element in list lst2 and converts it into a string using the str function. The resulting iterator is then cast into a list using the list() function.

We could have also used list comprehension and for-loop to convert each element of lst2 into a string using the line lst3 = [str(i) for i in lst2] instead of lst3 = list(map(str, lst2)).

Conclusion

This article covered three methods of printing Python lists without square brackets. The first method leverages the concept of unpacking list elements and passing them into a print statement.

This method works for both string and non-string data types; the string quotes are not displayed on the output.

The second method utilizes the concept of string slicing to eliminate the brackets in the printed output. Lastly, we discussed how to use the join() function to join list elements.