Print a list of integers in one line in Python

avatar

Borislav Hadzhiev

Last updated: Sep 3, 2022

banner

Photo from Unsplash

Print a list of integers in one line in Python #

Use the iterable unpacking operator to print a list of integers in one line, e.g. print(*list_of_integers). The iterable unpacking operator will unpack the list in the call to the print() function, printing the integers on a single line.

main.py
list_of_integers = [2, 4, 8] # ✅ print list of integers in one line (unpacking) print(*list_of_integers) # 👉️ 2 4 8 print(*list_of_integers, sep=', ') # 👉️ 2, 4, 8 # --------------------------------------- # ✅ print list of integers in one line (str.join()) result = ', '.join(str(item) for item in list_of_integers) print(result) # 👉️ 2, 4, 8

We used the iterable unpacking operator to print a list of integers in one line.

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

main.py
list_of_integers = [2, 4, 8] print(*list_of_integers) # 👉️ 2 4 8 print(*list_of_integers, sep=', ') # 👉️ 2, 4, 8k
By default, the list items are printed with a space separator because the sep argument is set to a space.

You can set the sep argument to any other value to print the integers with a different separator.

main.py
list_of_integers = [2, 4, 8] print(*list_of_integers, sep='-') # 👉️ 2-4-8 print(*list_of_integers, sep='-', end='!!!') # 👉️ 2-4-8!!!

The string we passed for the end keyword argument is inserted at the end of the string.

The end argument is set to a newline character (\n) by default.

Alternatively, you can use the str.join() method.

Print a list of integers in one line using str.join() #

To print a list of integers in one line:

  1. Convert each integer in the list to a string.
  2. Use the str.join() method to join the list into a string.
  3. Use the print() function to print the result.
main.py
list_of_integers = [2, 4, 8] result = ', '.join(str(item) for item in list_of_integers) print(result) # 👉️ 2, 4, 8

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

The join() method raises a TypeError if there are any non-string values in the iterable.

This is why we had to convert each item in the list to a string before calling join().

We used a generator expression to iterate over the list.

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 passed the current integer to the str() class and returned the result.

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

main.py
list_of_integers = [2, 4, 8] result = ' '.join(str(item) for item in list_of_integers) print(result) # 👉️ 2 4 8

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 = [2, 4, 8] result = ''.join(str(item) for item in list_of_integers) print(result) # 👉️ '248'
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.