Python print list – Using Loops, Maps | 4 Ways

Python List is the most frequently used and very versatile datatypes in Python. It’s easy to make a list but how you will do program a python code to print list elements?

Python print list Using Loops, Maps different Ways

In this tutorial, we will do a python program only for a print full list. Here are way some ways to print a list in python:-

Let’s start Print lists in Python

For loop Statment:-

The easy way to do it with for loop, it’s print all elements of the python list one by one.

Output: 1
2
3
4
5

While loop Statment:-

Same easy as for loop, In the below code we have a num list with 5 elements. Then one count variable to check condition. Then while loop has a condition run until true expression.

Output: 1
2
3
4
5

Convert a list to a string

As an upper example with loops, we see an example for a list with a number. But you can use the same method with the string list.

There is also an option for a string list to print. Using join() function but for string, you have to convert it to string list then use join() function.

Output: Python String example
1, 2, 3, 4, 5

map() function

Use map() function to convert each element of the list into a string if the list is not a string and then use join function to join them.

Output: 1 2 3 4 5

Question: Print a list of space-separated elements in Python 3?

Answer: To get this you have to apply the list as separate arguments: print(*List)

See below example

Output: 1 2 3 4 5
1, 2, 3, 4, 5
1 -> 2 -> 3 -> 4 -> 5

This is the easiest method Unless you need the joined string for something else. Otherwise, use str.join():

Do comment if you have a any doubt and suggestion on this tutorial.

Note:
IDE: PyCharm 2020.1.1 (Community Edition)
macOS 10.15.4
Python 3.7
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.