Print Elements of List on Separate Lines in Python

Lists are one of the fundamental data types in Python that is capable of storing several items in one single variable. Elements of the list are all generally printed in a single line. However, there is often a need to print all the elements on separate lines, which is what this article will be focused on.

This tutorial provides several different ways available to print elements of a list on separate lines in Python.

How To Print Elements of List on Separate Lines in Python?

There are various approaches, all of which can implement the task of printing elements of a list on separate lines in Python.

These approaches range from a simple for loop to string conversion and utilization of the join() function, with each and every approach being thoroughly defined in this article below.

Using a for Loop

A for loop can take a simple list and print each of the elements in a separate line. We simply make use of the for loop to traverse through each of the elements of the list and print them side by side.

It is one of the simplest methods to understand and can be implemented easily even by someone with entry-level knowledge of Python.

The following code uses a for loop to print elements of a list on separate lines in Python.

Output:

100
101
99
98
102

Using the * Symbol and The sep Parameter

With the print function, we can specify the separation between different elements of the list by making use of the sep parameter. The * symbol is utilized to represent all the elements of the list separated by a space in between each of them.

Both of these, when combined together, can implement the task at hand. When we specify the sep parameter value as a newline character (\n), it prints all the elements in different lines.

The following code uses the * symbol and the sep parameter to print elements of a list on separate lines in Python.

Output:

100
101
99
98
102

Using List Comprehension

List comprehension is a simpler way to create new lists with reference to values in an already existing list. List comprehension makes it easy to create lists in a single line rather than a whole block of code.

List comprehension is, in our case, capable of seamlessly displaying all the elements of the list in separate lines.

The following code uses list comprehension to print elements of a list on separate lines in Python.

Output:

100
101
99
98
102

Using the join() Function

When we utilize the join() function, we simultaneously convert the elements into a string and join all of them in a single one. Then, by utilizing the \n newline character along with it, we implement the task at hand.

The following code uses the join() function to print elements of a list on separate lines in Python.

Output:

Java
2
blog

It is important to note that this approach only works in the case where all the elements of the given list are in a string format. This approach returns an error when it encounters an int as a list element.

Using the map() Function Along with The join() Function

To counter the above issue of encountering an error in the case of an int while making use of the join() function, we utilize the map() function along with the join() function.

This prevents any error from occurring by manually converting all elements of a list to string, even if it was originally an integer value.

The following code uses the map() function along with the join() function to print elements of a list on separate lines in Python.

Output:

100
101
99
98
102

Conclusion

This tutorial focuses on and provides different approaches to solve the task of printing elements of a list on separate lines in Python.

The for loop is the simplest and the most utilized approach, along with several other methods that further dive deep into the world of Python, all of which have been explained in this article and implement the task at hand seamlessly.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *