How to Print a Python List?

Python is a famous programming language that is widely utilized for its simplicity and ease of use. One of the most common tasks/operations when working with Python is printing lists. The lists are a collection of values that can be efficiently accessed/called and manipulated.

In this article, we will explore various easy methods to print a Python list, including the use of the map() function, a for loop, and the join() method, etc. 

This guide explains the following easy ways to print a Python list:

Method 1: Using map() Function

The “map()” function is utilized to apply a particular function to each element/item of an iterable and retrieve a new iterable. The following code is used to print a Python list:

Code:

list_value = [15, 25, 35, 45]
print('\n'.join(map(str, list_value)))

The “map()” function accepts the string and list as an argument and applies a string function to each element of the list. This map() function retrieves a new iterable containing only a string. These strings are joined to an empty string using the “join()” function.

Output:

The list has been printed.

Method 2: Using the ‘*’ Operator

The Python “*” operator can be used to unpack an iterable into an individual element. The below code uses the “*” operator to print the list:

Code:

list_value = [15, 25, 35, 45]
print(*list_value, sep = "\n")

The “*list_value” expression is used to unpack the given list into separate elements. The returned unpacking value and the separator “\n” is passed in the “print()” function to display the list element value on newline.

Output:

The list has been printed.

Method 3: Using for Loop

The traditional for loop is used in the below code to print the given Python list:

Code:

list_value = ['A', 'B', 'C', 'D']
for ele in list_value:
    print(ele)

The for loop iterates over the list and displays the element value of the list.

Output:

The input list has been printed.

Method 4: Using join() Method

The below code uses the “join()” method to print the given list:

Code:

list_value = ['Alex', 'Cyndy', 'Lily']
print(' '.join(list_value))

The “join()” method accepts the list of strings as an argument and joins them to an empty string. 

Note: This method requires that the list must contain a string as an element otherwise error occurs.

Output:

The string list has been displayed.

Method 5: Using List Comprehension

The Python list comprehension is used to create the list based on the existing list. The below code uses list comprehension to print a Python list:

Code:

list_value = [54, 45, 98, 78]
[print(i) for i in list_value]

In the list comprehension, the for loop iterates over the list and for each iteration the “print()” function displays the element value of the list.

Output:

The input list has been printed.

Method 6: Using print() Function

A simple print() function in Python can also be used to print the list as shown in the following code:

Code:

list_value = [15, 25, 35, 45]
print(list_value)

The “print()” function accepts the list as an argument and returns the list.

Output:

The list has been printed.

Conclusion

To print the Python list, use the “map()” function, “* operator”, “for loop”, “join()” method, “list comprehension”, and the “print()” function. The “map()” function is used along with the “join()” function to print a Python list. The traditional for loop is also used to print a list by iterating over the list and using the “print()” function. The methods such as the * operator, join() method, and list comprehension is used to print a list. This post has provided various easy ways to print a list in Python using appropriate examples.