How to Print a List in Python

To print the contents of a list in Python, all you need to do is call the print() function on it.

Here is an example of printing a list of three fruits in a list:

fruits = ["Banana", "Apple", "Orange"]

print(fruits)

Output:

['Banana', 'Apple', 'Orange']

But if you want to print each element of a list separately, you should use a for loop.

For instance, let’s print the three elements of a list on separate lines by using a for loop:

fruits = ["Banana", "Apple", "Orange"]

for fruit in fruits:
    print(fruit)

Output:

Banana
Apple
Orange

These answers are probably enough, but in case you’re interested, feel free to keep on reading to learn more approaches to printing lists in Python.

Alternative Ways to Print a List in Python

Besides passing the list to a print function or using a loop to print each element, there are three great alternative ways to print list items in Python.

  1. Use the asterisk operator (*).
  2. Use the list.join() method.
  3. Use the map() function.

Let’s take a closer look at these approaches and why you might want to consider those.

1. Use an Asterisk to Print a List in Python

To print a list without commas or square brackets without using a loop, you can use the asterisk to unpack the list elements.

fruits = ["Banana", "Apple", "Orange"]

print(*fruits)

Output:

Banana Apple Orange

The asterisk operator in Python is known as the unpacking operator. You can use it to pull values from iterables, such as lists, strings, or tuples.

And by the way, if you want to have a separator between the printed list elements, specify sep parameter in the print() function call.

For instance, let’s print the list of elements using the unpacking operator and separate the elements by a comma and a blank space:

fruits = ["Banana", "Apple", "Orange"]

print(*fruits, sep=", ")

Output:

Banana, Apple, Orange

Notice that the separator can be anything.

For example, you can use this approach to print each element on a new line by:

fruits = ["Banana", "Apple", "Orange"]

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

Output:

Banana
Apple
Orange

2. Use the Built-In join() Method for Lists of Strings

If you have a list of strings, you can use the string.join() method to combine the strings and print the result.

For example:

fruits = ["Banana", "Apple", "Orange"]

print(" ".join(fruits))

Output:

Banana Apple Orange

Notice that the .join() method belongs to the string type. So this does not work for example when the list consists of integers.

3. Use the Built-In Map Function to Print a List in Python

To print a list of integers similar to how you printed the strings in the previous example:

  1. Use the map() function to transform them into strings.
  2. Call join() method to merge them into one string.
  3. Print the resulting string out using the print() function.

For instance:

nums = [1, 2, 3, 4, 5]
print(" ".join(map(str, nums)))

Output:

1 2 3 4 5

Let’s take a quick look at how this code works.

  1. The map() function creates a new list that contains each integer as a string.
  2. Then the join() method combines all the strings in the new list into one string that is printed out.

Conclusion

That’s a whole bunch of ways to print lists in Python.

Before you go, here is a quick recap of the things to take home.

The easiest way for you to print a list in Python is by just calling print() function on a list:

fruits = ["Banana", "Apple", "Orange"]
print(fruits)

To separately print each element, use a for loop:

fruits = ["Banana", "Apple", "Orange"]

for fruit in fruits:
    print(fruit)

Alternatively, you can use

  • String’s join() method.
  • Asterisk unpacking (*).
  • The map() function with the join() method.

Thanks for reading. I hope you find it useful.

Happy coding!

Further Reading

Leave a Comment

Your email address will not be published. Required fields are marked *