Print list elements on separate Lines in Python

avatar

Borislav Hadzhiev

Last updated: Sep 11, 2022

banner

Photo from Unsplash

Print list elements on separate Lines in Python #

To print the elements of a list on separate lines:

  1. Use a for loop to iterate over the list.
  2. Use the print() function to print each element.
  3. The items in the list will get printed on separate lines.
main.py
my_list = ['bobby', 'hadz', '.com'] for item in my_list: # bobby # hadz # .com print(item) # -------------------------------------- # bobby # hadz # .com print(*my_list, sep='\n') # -------------------------------------- result = '\n'.join(my_list) # bobby # hadz # .com print(result)

The first example uses a simple for loop to iterate over the list and print each element.

main.py
my_list = ['bobby', 'hadz', '.com'] for item in my_list: # bobby # hadz # .com print(item)

The print function takes one or more objects and prints them to sys.stdout.

By default, the print() function adds a newline (\n) character at the end of each statement, so it prints the items in the list on separate lines.

main.py
for item in ['bobby', 'hadz']: # bobby # hadz print(item) for item in ['bobby', 'hadz']: # bobby hadz print(item, end=' ')
The value of the end argument gets printed immediately after the message you pass to the print() function.

Alternatively, you can use the sep argument.

Print list elements on separate Lines using sep #

Use the sep argument to print the elements of a list on separate lines, e.g. print(*my_list, sep='\n'). The items in the list will get unpacked in the call to the print() function and will get printed on separate lines.

main.py
my_list = ['bobby', 'hadz', '.com'] # bobby # hadz # .com print(*my_list, sep='\n')
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.

You can set it to a newline (\n) character to print the list's items on separate lines.

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

Print list elements on separate Lines using str.join() #

To print the elements of a list on separate lines:

  1. If the list contains numbers, convert them to strings.
  2. Use the str.join() method to join the list with a newline (\n) character separator.
  3. Use the print() function to print the result.
main.py
list_of_strings = ['bobby', 'hadz', 'com'] # ✅ Print list of strings on separate lines result = '\n'.join(list_of_strings) # bobby # hadz # com print(result) # --------------------------------------------- # ✅ Print list of numbers on separate lines list_of_numbers = [16, 24, 88] result = '\n'.join(str(item) for item in list_of_numbers) # 16 # 24 # 88 print(result)

We used the str.join() method to print the items of a list on separate lines.

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_numbers = [16, 24, 88] result = '\n'.join(str(item) for item in list_of_numbers) # 16 # 24 # 88 print(result)

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.

For our purposes, we used a newline (\n) character.

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.