Wednesday, December 25, 2024
Google search engine
HomeLanguagesPython | Printing list vertically

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




# 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 

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




# 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

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




# initializing list
test_list = [[1, 4, 5], [4, 6, 8], [8, 3, 10]]
 
# printing original list
print("The original list is:", test_list)
 
# using a list comprehension
# to print list vertically
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
# initializing list
test_list = [[1, 4, 5], [4, 6, 8], [8, 3, 10]]
 
# printing original list
print("The original list is:", test_list)
 
test_list = np.array(test_list)
# using NumPy
# to print list vertically
result = np.transpose(test_list)
for i in result:
    print(*i)
#This code is contributed by Jyothi pinjala.


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
 
# initializing list 
test_list = [[1, 4, 5], [4, 6, 8], [8, 3, 10]]
# printing original list
print("The original list is:", test_list)
# creating a dataframe from list
df = pd.DataFrame(test_list)
 
# transposing the dataframe
df = df.transpose()
 
# printing the transposed dataframe as a list
for i in range(len(df)):
    print(*df.iloc[i].values)
#this code is contributed by vinay pinjala.


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:

  1. Initialize a list of lists test_list.
  2. Print the original list.
  3. Use the reduce method to transpose the list:
  4. Initialize the accumulator to the original list test_list.
  5. Iterate over each row of the accumulator and group together the corresponding elements from each row
  6. using the zip function.
  7. Convert the resulting tuples to lists and append them to a new accumulator list.
  8. Return the new accumulator list as the result of the reduce operation
  9. Print the resulting transposed list vertically.

Python3




from functools import reduce
 
# initializing list
test_list = [[1, 4, 5], [4, 6, 8], [8, 3, 10]]
 
# printing original list
print("The original list is:", test_list)
 
# using reduce method to transpose the list
result = reduce(lambda acc, x: [list(row) for row in zip(*acc)], test_list, test_list)
 
# print the transposed list vertically
for row in result:
    print(*row)
#This code is contributed by Rayudu.


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.

RELATED ARTICLES

Most Popular

Recent Comments