3 Ways to Print a List in Python [Step-by-Step]

Printing List In Python

Printing lists in Python is a fundamental operation that allows developers to display list elements individually. 

This can be useful in many situations. For instance, suppose you’re building a social media application that contains all of a user’s friends in a Python list. In that case, you’ll need to print the list to display them. This is just an example, the reason can be more such as printing the list’s elements for debugging, sharing data, outputting results, etc.

This tutorial will show you various techniques and methods to print all the elements of a list in Python. Let’s start with a short introduction to the list.

Introduction to Python List

Python offers us various in-built data structures to make storing and processing data much easier. A list is one of them. 

Python List is a typed array that can store duplicated elements, it is an ordered collection of data, dynamic in size, and mutable, and its elements can be accessed using list indexing. 

Since there is an ordered data collection, it becomes easy to print the contents of a list. Python has a lot to offer for this, let’s look at them one by one.

Print a List in Python using the map() Function

Python map() function can be clubbed with the join() function to print the Python list elements individually.

Syntax:

''.join(map(str,list))

Here, inside ‘ ‘, you can insert /n if you want to print each element of a list on the next line, or you can insert a comma(,) so that element is in one line separated by commas, or you can use any custom symbols.

Example:

lst = [10,20,30,'John',50,'Joe']

print("Elements of List:\n")

print('\n'.join(map(str, lst)))

Here we used the map() function to convert the integer & string data types present in the list to the string using str() as the mapping function. Then, we used the join() function to combine the elements into a single string with a newline character (‘\n’) used as the separator between list items. 

Output:

Elements of List:

10
20
30
John
50
Joe

In the above output, you can see that each element of the list is printed on a new line, hence the operation is successful.

Print a List in Python using the * Symbol

To make the process easier, we can use the * symbol to unpack the list elements and print it further. Let’s see how.

Syntax:

*list

We can customize the output by including the sep value. 

Example:

lst = [10,20,30,'John',50,'Joe']

print("Elements of List:\n")

print(*lst, sep = "\n")

Here we have passed sep = “\n” so that each element will be printed in a new line like in the previous example.

Output:

Elements of List:

10
20
30
John
50
Joe

Here we got the same output as above but this method is shorter and easier than the previous one as we use just one symbol whereas, in the previous method, we have to use three methods.

Print a List in Python using for loop

As a beginner, the naive approach is always the best to start with. In this method, we iterate over each element of the list one by one using for loop and print the element parallelly.

Syntax:

for element in list:
    print(element)

Example:

lst = [10,20,30,'John',50,'Joe']

print("Elements of List:\n")

for x in lst:
  print(x)

Here, for loop will iterate through the list one by one, and print the element on each iteration. For this method, each element will automatically be printed on a new line, so nothing extra is needed.

Output:

Elements of List:

10
20
30
John
50
Joe

In the above output, we got the same result as the previous results. Still, this approach seems simple and easy to understand for beginners as no additional parameters or methods are required here.

Conclusion

In this tutorial, we looked at three ways to print a Python list: using map(), using the * symbol, and using a for loop. All of them can be used accordingly, but if you want the simplest way that doesn’t use an extra concept then it’s best to use a traditional for loop. Hope you got enough information to be able to print a list of elements in Python.

References