Python | Printing list vertically
Last Updated :
21 Apr, 2023
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
test_list = [[ 1 , 4 , 5 ], [ 4 , 6 , 8 ], [ 8 , 3 , 10 ]]
print ("The original list is : " + str (test_list))
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
Time Complexity: O(n*n), where n is the length of the list test_list
Auxiliary Space: O(1) constant additional space required
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
test_list = [[ 1 , 4 , 5 ], [ 4 , 6 , 8 ], [ 8 , 3 , 10 ]]
print ("The original list is : " + str (test_list))
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
Method #3 : Using a list comprehension:
This approach uses a list comprehension to iterate over the elements of each sublist in test_list and store them in the result list. It then uses a loop to print the elements of result in vertical form, with each sublist being printed on its own line.
The time complexity of this approach is O(n), where n is the number of elements in test_list. The space complexity is also O(n), as the result list is stored in memory.
Python3
test_list = [[ 1 , 4 , 5 ], [ 4 , 6 , 8 ], [ 8 , 3 , 10 ]]
print ( "The original list is:" , test_list)
result = [row[i] for i in range ( len (test_list[ 0 ])) for row in test_list]
for i in range ( 0 , len (result), len (test_list)):
print ( * result[i:i + len (test_list)])
|
Output
The original list is: [[1, 4, 5], [4, 6, 8], [8, 3, 10]]
1 4 8
4 6 3
5 8 10
Method #4 : Using Numpy
Python3
import numpy as np
test_list = [[ 1 , 4 , 5 ], [ 4 , 6 , 8 ], [ 8 , 3 , 10 ]]
print ( "The original list is:" , test_list)
test_list = np.array(test_list)
result = np.transpose(test_list)
for i in result:
print ( * i)
|
Output:
The original list is: [[1, 4, 5], [4, 6, 8], [8, 3, 10]]
1 4 8
4 6 3
5 8 10
Time complexity: O(n*m)
Auxiliary Space: O(n*m)
Method#5: Using Pandas
Python3
import pandas as pd
test_list = [[ 1 , 4 , 5 ], [ 4 , 6 , 8 ], [ 8 , 3 , 10 ]]
print ( "The original list is:" , test_list)
df = pd.DataFrame(test_list)
df = df.transpose()
for i in range ( len (df)):
print ( * df.iloc[i].values)
|
Output
The original list is: [[1, 4, 5], [4, 6, 8], [8, 3, 10]]
1 4 8
4 6 3
5 8 10
Time complexity: O(n*m)
Auxiliary Space: O(n*m)
Method#6: Using reduce():
Algorithm:
- Initialize a list of lists test_list.
- Print the original list.
- Use the reduce method to transpose the list:
- Initialize the accumulator to the original list test_list.
- Iterate over each row of the accumulator and group together the corresponding elements from each row
- using the zip function.
- Convert the resulting tuples to lists and append them to a new accumulator list.
- Return the new accumulator list as the result of the reduce operation
- Print the resulting transposed list vertically.
Python3
from functools import reduce
test_list = [[ 1 , 4 , 5 ], [ 4 , 6 , 8 ], [ 8 , 3 , 10 ]]
print ( "The original list is:" , test_list)
result = reduce ( lambda acc, x: [ list (row) for row in zip ( * acc)], test_list, test_list)
for row in result:
print ( * row)
|
Output
The original list is: [[1, 4, 5], [4, 6, 8], [8, 3, 10]]
1 4 8
4 6 3
5 8 10
Time complexity:
The time complexity of this code is O(nm), where n is the number of rows and m is the number of columns in the original list. This is because the reduce method iterates over each row of the original list once, and the zip function used to group the corresponding elements from each row together takes O(m) time. Therefore, the time complexity grows linearly with the number of rows and columns in the list.
Space complexity:
The space complexity of this code is O(nm), because it creates a new list of the same size as the original list to store the transposed result. Therefore, the space used by this code grows linearly with the number of rows and columns in the list. Additionally, the reduce method and the zip function used in the lambda function also use some constant additional memory, but this is negligible compared to the size of the input list.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...