Print the elements of a list in one line in Python

avatar

Borislav Hadzhiev

Last updated: Sep 11, 2022

banner

Photo from Unsplash

Print the elements of a list in one line in Python #

Use the iterable unpacking operator to print the elements of a list in one line, e.g. print(*list_of_strings, sep=' '). The iterable unpacking operator will unpack the list's elements in the call to the print() function and will print them in a single line.

main.py
list_of_strings = ['bobby', 'hadz', 'com'] print(*list_of_strings, sep=' ') # 👉️ bobby hadz com print(*list_of_strings, sep=', ') # 👉️ bobby, hadz, com print(*list_of_strings, sep='') # 👉️ bobbyhadzcom # ----------------------------------------------------- list_of_integers = [43, 21, 65] print(*list_of_integers, sep=' ') # 👉️ 43 21 65 print(*list_of_integers, sep=', ') # 👉️ 43, 21, 65 print(*list_of_integers, sep='') # 👉️ 432165

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', sep=', ') # 👉️ bobby, hadz print('bobby', 'hadz') # 👉️ bobby hadz

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

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

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

To print the elements of a list in one line:

  1. Use the str.join() method to join the list into a string.
  2. If the list contains numbers, convert them to strings.
  3. Use the print() function to print the string.
main.py
# ✅ print list of strings in one line list_of_strings = ['bobby', 'hadz', 'com'] result = ', '.join(list_of_strings) print(result) # 👉️ bobby, hadz, com # ----------------------------------------------------- # ✅ print list of integers in one line list_of_integers = [43, 21, 65] result = ', '.join(str(item) for item in list_of_integers) print(result) # 👉️ 43, 21, 65

We used the str.join() method to print the items of a list in one line.

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().

main.py
list_of_integers = [43, 21, 65] result = ', '.join(str(item) for item in list_of_integers) print(result) # 👉️ 43, 21, 65

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 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 = [43, 21, 65] result = ' '.join(str(item) for item in list_of_integers) print(result) # 👉️ 43 21 65

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_strings = ['bobby', 'hadz', 'com'] result = ''.join(list_of_strings) print(result) # 👉️ bobbyhadzcom

You can also use the map() function to convert all items in the list to strings before calling join().

main.py
list_of_integers = [43, 21, 65] result = ' '.join(map(str, list_of_integers)) print(result) # 👉️ 43 21 65

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

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.