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.
# 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() |
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.
# 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) |
Output :
The original list is : [[1, 4, 5], [4, 6, 8], [8, 3, 10]] 1 4 8 4 6 3 5 8 10
Recommended Posts:
- Multi-Line printing in Python
- Python | Avoiding quotes while printing strings
- Programs for printing pyramid patterns in Python
- Python | Convert list of tuples to list of list
- Python | Convert list of string to list of list
- Object Oriented Programming in Python | Set 2 (Data Hiding and Object Printing)
- Python program to create a list of tuples from given list having number and its cube in each tuple
- Python | Find maximum length sub-list in a nested list
- Python | Convert list of string into sorted list of integer
- Python | Check if a list exists in given list of lists
- Python | Ways to sum list of lists and return sum list
- Python | Remove all values from a list present in other list
- Python | Maximum sum of elements of list in a list of lists
- Python | Maximum absolute difference list of list
- Python | Convert a string representation of list into list
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.



