How to print a list in Python?
Python has many in-built data structures to make programming faster and more efficient than any other language. All these data structures are mutable and sequential in nature and store huge data collection in various formats. Python’s list data structure is built for simplicity and flexibility. In this tutorial, we will discuss various ways to print a list in Python.
Table of contents
- Introduction
- Using the * symbol to print a list
- The join() function to print a list
- Using the map() function to print a list in Python
- In Python, print list using for loop
- Closing thoughts
Introduction to Python print list
A list can contain any number of elements of different data types such as integer, float, string, etc. and in Python, printing a list is a simple task that can be executed using various methods. This tutorial covers some of the methods to print lists in Python.
Using the * symbol to print a list in Python
To print the contents of a list in a single line with space, * or splat operator is one way to go. It passes all of the contents of a list to a function.
We can print all elements in new lines or separated by space and to do that, we use sep=”\n” or sep=”, ” respectively. The below example illustrates this.
Input:
list = [Jon, Ned, 8, 99]
print (*list)
print ("printing lists separated by commas")
print (*list, sep=",")
print ("printing lists in new line")
print (*list, sep="\n")
Output:
Jon Ned 8 99
printing lists separated by commas
Jon, Ned, 8, 99
printing lists in new line
Jon
Ned
8
99
Using join() to print a list in Python
If the list contains strings as elements, then using the join() function is the smart choice. It simply joins the strings and then prints the list. If the list has integers, then this function will join them to a string first and then print the string. This example shows how it's done:
Input:
list = ["Jon", "Ned", "Arya"]
print (' '.join(list))
list_1 = [1, 2, 3, 4, 5]
print str(list_1)[1:-1]
Output:
John Ned Arya
1, 2, 3, 4, 5
Using map() function to print list in Python
The map() function takes two arguments: a function to apply to each element of a list, and a list. It returns a map object (which is an iterator) after applying the given function to each item of a given iterable. Then the join() function joins the elements of the list using a separator as mentioned in the earlier method. Here's how it's done:
Input:
list = [1, 2, 3, 4, 5]
print(' '.join(map(str, list)))
Output:
1 2 3 4 5
In Python, print list using for loop
One of the standard methods to print a list in Python is using the for loop. You can traverse the list from the 0th index to len(list) and print all the elements in the sequence.
Input:
list = ["Jon", "Ned", "Arya"]
for i in range(0, len(list)):
print(list[i])
Output:
Jon
Ned
Arya
Moreover, you can also print the list using for loop without mentioning the range as shown below:
Input:
list = ["Jon", "Ned", "Arya"]
for item in list:
print(item)
Output:
Jon
Ned
Arya
Closing thoughts
Printing the list is one of the everyday tasks while coding your program and getting the expected output. Therefore, we discussed different ways in Python to print the list. One can learn about other tasks related to the Python list here.