In Python, we can print a list easily. We can use loops, the Python join() function, or the Python * operator to print all elements of a list.

The first example shows how to print a list using a loop.

list = ["This","is","a","list","of","strings"]

for x in list:
    print(x)

#Output:
This
is
a
list
of
strings

You can also use the Python join() function to print the items of a list.

list = ["This","is","a","list","of","strings"]

print(" ".join(list))

#Output:
This is a list of strings

Another method is you can use the Python * operator to print a list in Python.

list = ["This","is","a","list","of","strings"]

print(*list)

#Output:
This is a list of strings

Finally, if you have elements which aren’t strings in your list, you can use the map() function.

list = ["This","is","a","list","of","strings",1,2,3,4,5]

print(" ".join(map(str, list))) 

#Output:
This is a list of strings 1 2 3 4 5

When working with collections, it is useful to be able to print the items of a list or dictionary.

We can easily print the elements of a list in Python using a number of different methods.

We can print a list by using the Python print() function. This will print the list object to the console.

list = ["This","is","a","list","of","strings"]

print(list)

#Output:
["This","is","a","list","of","strings"]

Typically, we want to get only the elements of the list, and not the list. To print the items of a list to the console with Python, we can use a for loop to print the elements of a list.

list = ["This","is","a","list","of","strings"]

for x in list:
    print(x)

#Output:
This
is
a
list
of
strings

Using the Python join() Function to Print List in Python

Another method for printing a list in Python is to use the Python join() function. You can use the join() function on a delimiter string, and pass a list as an argument to join the elements of the list by the delimiter.

To print a list with the join() function, we first join the elements of the list by spaces, and then pass it to print().

list = ["This","is","a","list","of","strings"]

print(" ".join(list))

#Output:
This is a list of strings

Using the Python * Operator to Print List

Another method that we can print a list in Python is to use the Python * operator. The * operator unpacks each element of a list.

We can use the Python * operator to print a list as shown in the following Python code.

list = ["This","is","a","list","of","strings"]

print(*list)

#Output:
This is a list of strings

If you want to print the elements of a list on different lines, you can pass ‘n’ to the optional “sep” argument.

list = ["This","is","a","list","of","strings"]

print(*list, sep='n')

#Output:
This
is
a
list
of
strings

Using the Python map() Function to Print List in Python

Finally, we use the Python map() function in combination with the Python join() function to print the elements of a list.

The map() function is useful if your string has items which are not strings because it will convert the elements to string variables before joining them.

Below is an example in Python of how to print a list using the map() function.

list = ["This","is","a","list","of","strings",1,2,3,4,5]

print(" ".join(map(str, list))) 

#Output:
This is a list of strings 1 2 3 4 5

Printing the First N Elements of a List in Python

In Python, we can easily print the first n items in a list. To do so, we can use a loop as shown in the first example of this article.

To print the first n items of a list, we just loop over those first n elements and print them out.

Below is an example which prints out the first 10 items of a list in Python.

list = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]

for i in range(0,10):
    print(list[i])

#Output:
0
1
2
3
4
5
6
7
8
9

Hopefully this article has been useful for you to understand how to print lists using Python.

Categorized in:

Python,

Last Update: February 26, 2024