Print a list of integers without brackets in Python

avatar

Borislav Hadzhiev

Last updated: Sep 11, 2022

banner

Photo from Unsplash

Print a list of integers without brackets in Python #

To print a list of integers without brackets:

  1. Use a generator expression to iterate over the list.
  2. Use the str() class to convert each integer to a string.
  3. Use the str.join() method to join the list into a string.
  4. Use the print() function to print the result.
main.py
list_of_integers = [13, 55, 73] result = ''.join(str(item) for item in list_of_integers) print(result) # 👉️ 135573 result = ' '.join(str(item) for item in list_of_integers) print(result) # 👉️ 13 55 73 result = ', '.join(str(item) for item in list_of_integers) print(result) # 👉️ 13, 55, 73

We used the str.join() method to print a list of integers without square brackets.

The str.join method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.

Note that the method raises a TypeError if there are any non-string values in the iterable.

If your list contains numbers or other types, convert all of the values to strings before calling join().

We used a generator expression to iterate over the list

main.py
list_of_integers = [13, 55, 73] result = ', '.join(str(item) for item in list_of_integers) print(result) # 👉️ 13, 55, 73
Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we use the str() class to convert the number to a string.

The string the join() method is called on is used as the separator between the elements.

main.py
list_of_integers = [13, 55, 73] result = ' '.join(str(item) for item in list_of_integers) print(result) # 👉️ 13 55 73

If you don't need a separator and just want to join the list's elements into a string, call the join() method on an empty string.

main.py
list_of_integers = [13, 55, 73] result = ''.join(str(item) for item in list_of_integers) print(result) # 👉️ 135573

An alternative way to convert the integers in the list to strings is to use the map() function.

main.py
list_of_integers = [13, 55, 73] result = ''.join(map(str, list_of_integers)) print(result) # 👉️ 135573 result = ' '.join(map(str, list_of_integers)) print(result) # 👉️ 13 55 73 result = ', '.join(map(str, list_of_integers)) print(result) # 👉️ 13, 55, 73

The map() function takes a function and an iterable as arguments and calls the function with each item of the iterable.

Alternatively, you can use the sep argument in the call to the print() function.

Print a list of integers without brackets using sep #

Use the sep argument to print a list of integers without brackets, e.g. print(*list_of_integers, sep=', '). The items in the list will get unpacked in the call to the print() function and will get printed without the square brackets.

main.py
list_of_integers = [13, 55, 73] print(*list_of_integers, sep='') # 👉️ 135573 print(*list_of_integers, sep=' ') # 👉️ 13 55 73 print(*list_of_integers, sep=', ') # 👉️ 13, 55, 73
Notice that we used the iterable unpacking * operator to unpack the items of the list in the call to print().

The * iterable unpacking operator enables us to unpack an iterable in function calls, in comprehensions and in generator expressions.

The sep argument is the separator between the arguments we pass to print().

main.py
print('bobby', 'hadz', sep='') # 👉️ bobbyhadz print('bobby', 'hadz') # 👉️ bobby hadz

By default, the sep argument is set to a space.

A less flexible approach is to convert the list to a string and use string slicing.

main.py
list_of_integers = [13, 55, 73] result = str(list_of_integers)[1:-1] print(result) # 👉️ 13, 55, 73

We used the str() class to convert the list of integers to a string and used string slicing to exclude the square brackets.

The syntax for string slicing is my_str[start:stop:step].

The start index is inclusive, whereas the stop index is exclusive (up to, but not including).

Python indexes are zero-based, so the first character in a string has an index of 0, and the last character has an index of -1 or len(my_str) - 1.

We used a start index of 1 to exclude the left square bracket and used a stop index of -1 to exclude the right square bracket.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.