How to Print a List in Python

To print a list in Python, call the print() function on it:

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

print(fruits)

This prints the whole list as-is:

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

If you want to print each element separately, use a for loop:

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

for fruit in fruits:
    print(fruit)

Output:

Banana
Apple
Orange

Although you are probably fine with these approaches already, stay tuned to see the other alternatives too.

Alternative Ways to Print a List in Python

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

If you want to have a separator between each element, specify sep in the print() call:

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

print(*fruits, sep=", ")

Output:

Banana, Apple, Orange

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

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

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

For example:

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

print(" ".join(fruits))

Output:

Banana Apple Orange

But this does not work for example when the list consists of integers. If you want to join a list of integers into a single string and print it out, use the map() function with join() method.

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

If you want to print a list of integers, you can use a map() function to transform them into strings. Then you can use the join() method to merge them into one string and print them out.

For example:

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

Output:

1 2 3 4 5

This particular example works such that 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 to 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)

If you want 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.

Thanks for reading. I hope you find it useful.

Happy coding!

Further Reading

50 Python Interview Questions and Answers

50+ Buzzwords of Web Development

Share on facebook
Facebook
Share on google
Google+
Share on twitter
Twitter
Share on linkedin
LinkedIn
Share on pinterest
Pinterest

Leave a Comment

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