In this article, we will discuss how to print list elements on separate lines in Python.
Table Of Contents
Introduction
Suppose we have a list of strings like this,
listOfStrs = ['Hello', 'this', 'is', 'a', 'correct', 'way', 'to', 'code']
Now we want to print each element of this list in a separate line. The output should be like this,
Hello this is a correct way to code
There are different ways to do this. let’s discuss them one by one,
Method 1: Using a for loop
We can iterate over all the elements in the list using a for loop, and print them one by one using the print() function. It will print each element of the list in a separate line.
Let’s see the example
Frequently Asked:
- How to reverse a List in Python?
- Check if any element in List starts with string in Python
- Python List count() method
- Check if all values in List are False in Python
listOfStrs = ['Hello', 'this', 'is', 'a', 'correct', 'way', 'to', 'code'] # print list items on separate lines for elem in listOfStrs: print(elem)
Output:
Hello this is a correct way to code
It printed each item of the list in a separate line.
Method 2: Using print() function
We can apply the astrik operator on the list object to decouple all the list elements as a separate items, and pass them as arguments to the print() function. But along with these arguments we will also pass another argument sep
. This argument contains the separator that will be used by the print() function, while printing each element on the console. As we want to print every element of the list in a separate line so we can pass in a new line character as a separator to the print() function.
Let’s see an example,
listOfStrs = ['Hello', 'this', 'is', 'a', 'correct', 'way', 'to', 'code'] # print list items on separate lines print(*listOfStrs, sep='\n')
Output:
Hello this is a correct way to code
It printed each item of the list in a separate line
Method 3: Using join() function
This is an interesting technique. In this method we will convert our list to a string by joining all the string elements of the list. We will use the new line character as a separator. Basically we will create a big string which contains all the strings from the list, separated by new line characters, and we will print that string on the console.
From the output on the console, it will seem that we have printed each string from the list in a separate line or element from a string on the separate line. Let us see the example,
Example 1
listOfStrs = ['Hello', 'this', 'is', 'a', 'correct', 'way', 'to', 'code'] # create a string that contains list items on separate lines strValue = '\n'.join(listOfStrs) print(strValue)
Output:
Hello this is a correct way to code
It printed each string from the list in a separate line.
But what if our list has some integers? Then this join() method will not work, because join() method expects the list to have only strings. In case list has some other type of elements, then we need to convert them to string first and then use the join() method to create a single string, in which all elements are separated by new line character. Let’s see an example,
Example 2
listOfNum = [21, 22, 23, 24, 25, 26, 27] # create a string that contains list items on separate lines strValue = '\n'.join(map(str, listOfNum)) print(strValue)
Output:
21 22 23 24 25 26 27
Summary
We learned about different ways to print list elements on separate lines in Python. Thanks.