Python | Printing list vertically

Printing the list has been dealt many times. But sometimes we need a different format to get the output of list. This also has application in getting a transpose of matrix. Printing list vertically also has application in web development. Lets discuss certain ways in which this task can be achieved.

Method #1 : Using Naive Method
The naive method can be used to print the list vertically vis. using the loops and printing each index element of each list successively will help us achieve this task.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate 
# Vertical list print 
# using naive method 
  
# initializing list  
test_list = [[1, 4, 5], [4, 6, 8], [8, 3, 10]]
  
# printing original list
print ("The original list is : " + str(test_list))
  
# using naive method  
# to print list vertically
for i in range(len(test_list)):
    for x in test_list:
        print(x[i], end =' ')
    print()

chevron_right


Output :

The original list is : [[1, 4, 5], [4, 6, 8], [8, 3, 10]]
1 4 8 
4 6 3 
5 8 10 

Method #2 : Using zip()
Using zip function, we map the elements at respective index to one other and after that print each of them. This performs the task of vertical printing.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate 
# Vertical list print 
# using zip()
  
# initializing list  
test_list = [[1, 4, 5], [4, 6, 8], [8, 3, 10]]
  
# printing original list
print ("The original list is : " + str(test_list))
  
# using zip() 
# to print list vertically
for x, y, z in zip(*test_list):
    print(x, y, z)

chevron_right


Output :

The original list is : [[1, 4, 5], [4, 6, 8], [8, 3, 10]]
1 4 8
4 6 3
5 8 10


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.