
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.
- Use the asterisk operator (
*
). - Use the
list.join()
method. - 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:
- Use the
map()
function to transform them into strings. - Call
join()
method to merge them into one string. - 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.
- The
map()
function creates a new list that contains each integer as a string. - Then the
join()
method combines all the strings in the new list into one string that is printed out.
Conclusion
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 thejoin()
method.