Python Program to Print List (7 Different Ways)

In this article, we will learn to write python program to print list in different ways –

1. Using for Loop

Using for loop, we can print item in list as below –

sampleList = [100, 101, 102, 103, 104, 105, 106, 107]

for item in sampleList:
    print(item)

In each iteration, variable item contains list-item present at that index in list. Then, we print value in item using built-in method – print().

Run above program, output will be –

100
101
102
103
104
105
106
107

2. Using for Loop and range(), len() methods

We can use for loop and some built-in methods – range() and len() to iterate elements in list and print it using print() method in python.

a = [10, 11, 12, 13, 14, 15, 16, 17]

for index in range(len(a)):
    print(a[index])

Here,

  1. range function returns range of elements from 0 to given length.
  2. len() method returns length of given list. Here, it returns length of list a.
  3. for loop iterates over values returned by range() method. Value at index in each iteration represents position of element in list. We can get item at that position using a[index].
  4. Finally, print(a[index]) prints value to console.

When you run above program, output will be –

10
11
12
13
14
15
16
17

3. Using while Loop

Like for loop, while loop can also be used to print elements in list. It can be done as below –

sampleList = ["Hi", "Hello", "Tutorialwing"]
pos = 0
while pos < len(sampleList):
  print(sampleList[pos])
  pos = pos + 1

Here,

  1. sampleList is a list in python. There are three values assigned to it – Hi, Hello and Tutorialwing
  2. Variable pos represents position at each iteration in while loop. It will be used to access element at that position in list – sampleList.
  3. len() method returns length of sample list.
  4. sampleList[pos] contains element present at index pos
  5. In each iteration, we print element at that position. Then, increase position by 1 and repeat loop iteration.

4. Using List Comprehension

We can print all elements of list using list comprehension as below –

sampleList = ["Hi", "Hello", "Tutorialwing"]
[print(x) for x in sampleList]

When you run above program, output is –

Hi
Hello
Tutorialwing

5. Print List Without Using Loop

Elements in list can be printed without using for or while loop. We can do so using * symbol.

  • Method 1

    sampleList = [11, 12, 13, 14, 15]
    
    print(*sampleList)
    

    Notice that we have used * symbol as prefix with name of the list.

    When above program is run, output is:

    11 12 13 14 15
    
  • Method 2

    If we want to print elements of list separated by commas, we can do so by using separator , in print() method.

    sampleList = [11, 12, 13, 14, 15]
    
    print(*sampleList, sep = ", ")
    

    When above program is run, output is:

    11, 12, 13, 14, 15
    
  • Method 3

    If we want to print each element of list in new line, we can do so by using separator “\n” as shown below –

    sampleList = [11, 12, 13, 14, 15]
    
    print(*sampleList, sep = "\n")
    

    When above program is run, output is:

    11
    12
    13
    14
    15
    

6. Print List (By Converting to String)

If there is list of string or number, we can convert it to string and print it.

  1. If List has string elements, we can simply join them using join() function. Then, print string as shown below –

    sampleList =["Hello", "Hi", "Tutorialwing"]
      
    print(' '.join(sampleList))
    

    Notice that sampleList has list of string elements. We have joined them using join() method. Then, printed it.

    When above program is run, output is:

    Hello Hi Tutorialwing
    
  2. If List has Integer elements, we can convert it to string. Then, print them using print() method as shown below:

    sampleList = [13, 23, 33, 43, 53]
      
    print(str(sampleList)[1:-1])
    

    Run above program, output is:

    13, 23, 33, 43, 53
    

7. Print List (By Using map())

If list does not contain string, we can convert them into string using map then print them as shown below –

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

At first, we applied map method and convert each element in list to string. str does that task to convert to string format.

After that, we joined them using join() method.

When we run above program, output is:

1 2 3 4 5

That’s how we can write python program to print list.

Leave a Reply