Printing a list vertically in Python

Python language is composed of various data structures and among them, the very common one is the list data structure. The list can take elements of integers or strings within the single or double quotes and it is mentioned inside the square brackets. When running a program, the outputs get printed in standard format but this article deals with returning the output in vertical form as a separate list. The elements can be identified by the index value of the list data structure.

Printing a list vertically

The elements in the list are arranged in some order and after assigning the data it cannot be changed. The basic structure of the list with the elements is given below,


list1 = [1, 3, “Hello”, “yes”] 

When the above statement gets printed, it will be in horizontal format.


Approach 1 − Using Nested for loop

Approach 2 − Using the itertools module

Approach 3 − Using Class method

Approach 1: Python Program to print the list in a vertical manner using nested for loop

The Nested for loop is used to give the inputs in a vertical structure. The time complexity of the below code is 0(n^2) as it uses two nested for loops. The outer loop iterates through the sublist and each item is iterated using the inner loop. The number of iterations equals the square of the length of the given list.


  • Step 1 − The list input is defined with integers and string values.

  • Step 2 − The for loop is used to iterate through the list and again the nested for loop is used for iteration.

  • Step 3 − The output is printed as vertical lists according to the input.


#initializing the list along with the sublist of elements in the form of integer and string
list1 = [[20, 40, 50], [79, 80], ["Hello"]]
#for loop is used to iterate through the list of elements 
for listmethod in list1:
	#nested for loop to iterate through the list using the item
	for item in listmethod:
    #print statement will return the item in terms of a list
	   print("[", item, "] ")



Approach 2: Python Program to print the list in a vertical manner using itertools library

The itertools module is used which makes the iteration process easier even for complex operations. From this module, the zip_longest function is made used to print the list vertically. The Time complexity of the mentioned code is 0(n). After the iteration process, the separate list is grouped using the join function.


  • Step 1 − The input is defined with a certain integer list of elements in the list.

  • Step 2 − The itertools library is imported to get the iteration process easier.

  • Step 3 − The zip_longest function is utilized to iterate through the lists, to fill the blanks.

  • Step 4 − Then it checks for any elements with space in the given list.

  • Step 5 − Based on the space, if space is given it returns the elements as such and in case of elements without spaces it inserts the space.


#initializing the list of elements
list1 = [[20, 40, 50], [79, 80], ["Hello"]]
#importing the module
import itertools
#for loop used for iteration 
#zip_longest function to deal with the spaces
for a in itertools.zip_longest(*list1, fillvalue= " "):
	if any(b != " " for b in a):
    #returns the final output in vertical structure
	   print(" ".join(str(b) for b in a))


20 79 Hello
40 80

Approach 3: Python Program to print the list in vertical manner using Class method

The class is defined to get the string prompted by the user to print the string in the form of a vertical structure. To do so, the functions are defined with each argument. The time complexity of function1 is 0(1) as it takes one string and the time complexity of function2 is 0(n).


  • Step 1 − The class is defined and later two functions are provided with one argument.

  • Step 2 − The function1 is defined with a string data type.

  • Step 3 − The for loop is used to iterate through the string given by the user.

  • Step 4 − Then the functions are called to get the string and the characters of the string.


#defining the class as string_ver
class string_ver:
#defining two functions as function1 and function2
	def function1(self):
 		self.list = ['20', '40', '50', '79', '80', "Hello"]
	def function2(self):
		for a in self.list:
			print("\t " + a)
string = string_ver()




The list data structure can be initialized with numbers, strings, or float numbers. The inputs defined are aligned in vertical form with the use of nested loops, by importing the itertools module and also using the class definition. The approaches are explained from the simple method to the difficulty level.

Updated on: 29-Aug-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started