Python Print List with Advanced Examples

Like any other programming language, Python has built-in data structures that make it faster and better at what it does. All these data structures are sequential in nature, and Python Print list is one of the most simple yet powerful. It can store a wide range of data under a single variable.

This article covers various ways to print a list in Python, with advanced examples and output.

What is print() in Python?

The Python print statement has been replaced by the print() function, which uses keyword arguments to replace most of the special syntax of the old print statement.

The built-in method documentation provides further information:

Print objects to the text stream file, separated by sep and followed by endsependfile, and flush, if present, must be given as keyword arguments.

print function

What is a list in Python?

A Python Print list is a data structure that stores multiple items in one variable. Items are separated by commas and enclosed in square brackets. Lists can include any number of elements with various data types, including integer, float, text, etc.

How do you print a list on one line in Python?

The print method takes an extra parameter, end=" ", to keep the pointer on the same line. This is used to print each element in the same line with a space after it.

x = [1, 2, 3, 4, 5]
for i in x:
   print(i, end=" ")

Output:

1 2 3 4 5

Several Ways to Print a List in Python

The following is a list of different ways to print a list in Python.

Using for loop

To print each element of the list sequentially, use a for loop beginning at 0 and ending at len(list).

Example:

x = [10, 20, 30, 40, 50]

for i in range(len(x)):
    print(x[i])

Output:

10
20
30
40
50

Without using loops

To print a list on one line with a space between each element, use the * symbol. To print each element on a separate line, utilize sep="\n". Use the sep=", " notation when you want to print each element with a space between them.

Example:

x = [10, 20, 30, 40, 50]

print(*x)  # this will print the list without comma

print("Separated by Commas: ")
print(*x, sep=", ")  # this will print the list by comma

print("Separated by New Line: ")
print(*x, sep="\n")  # this will print the list by new line

Output:

10 20 30 40 50
Separated by Commas:
10, 20, 30, 40, 50
Separated by New Line: 
10
20
30
40
50

Convert a list to a string for display

To join a list of strings, we can use the join() function. To join a list of integers, we first need to turn it into a string, then use the join() function.

Example:

x = ["Python", "for", "Free!"]

print(' '.join(x))  # this will print the string

x = [10, 20, 30, 40, 50]

print(str(x)[1:-1])  # this will print the number by converting it to string

Output:

Python for Free!
10, 20, 30, 40, 50

Using map method

If the items in the list are not strings, use the map() function to print each item into a string. The join() method is then used to combine the strings.

Example:

x = [10, 20, 30, 40, 50]

print(' '.join(map(str, x)))  # this print the number in the list
print('\n'.join(map(str, x)))  # this print the number in the list by new line

Output:

10 20 30 40 50
10
20
30
40
50

Using list comprehension

Convert each item in the list individually by using list comprehension.

Example:

x = [10, 20, 30, 40, 50]
[print(i, end=' ') for i in x]

print("\nBelow is printing using newline")
[print(i) for i in x]

Output:

10 20 30 40 50 
Below is printing using newline
10
20
30
40
50

Summary

The list is the most widely used sequential data structure in Python programming, and printing a list is a common task for programmers. We’ve shown you more than one method to help you get better at coding.

Leave a Comment