How to print a list into 4 columns horizontally and vertically (transposed)?

Question:

I have a list

list = [21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 39, 39, 44, 42, 40, 43, 47, 45, 57, 52, 75, 73, 86, 78, 70, 77, 87, 79, 113, 96, 147, 141, 170, 150, 130, 145, 167, 147, 225, 184]

and I am trying to get it to be returned in 4 columns horizontally

21    22    23    24
25    26    27    28
29    30    39    39
44    42    40    43
...

as well as vertically

21   39   75   147
22   39   73   141
23   44   86   170
24   42   78   150
25   40   70   130
26   43   77   145
27   47   87   167
28   45   79   147
29   57   113  225
30   52   96   184

This is my attempt at solving this horizontally:

# prints array in columns
def printArray(array):
    row = 10
    col = 4

    for i in range(row):
        for j in range(col):
            print(array[i][j], 't', end="")

But it does not work.

I’ve also tried searching how to transpose the list some how but only found how to transpose a list of lists.

Transpose list of lists

Any help would be appreciated thank you.

Asked By: Puggy

||

Answers:

Try doing it this way:

def printArray(array):
    row = 10
    col = 4

    #Horizontally
    print("Horizontally:n")
    for i in range(0,len(array)-col,col):
        for j in range(i,i+col):
            print(array[j], 't', end="")
        print()
    print()

    print("Vertically:n")
    #Vertically
    for i in range(0,row):
        for j in range(i,i+10*4,row):
            print(array[j], 't', end="")
        print()


lst = [21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 39, 39, 44, 42, 40, 43, 47, 45, 57, 52, 75, 73, 86, 78, 70, 77, 87, 79, 113, 96, 147, 141, 170, 150, 130, 145, 167, 147, 225, 184]

printArray(lst)

Output:

Horizontally:

21      22      23      24 
25      26      27      28 
29      30      39      39 
44      42      40      43 
47      45      57      52 
75      73      86      78 
70      77      87      79 
113     96      147     141 
170     150     130     145 

Vertically:

21      39      75      147 
22      39      73      141 
23      44      86      170 
24      42      78      150 
25      40      70      130 
26      43      77      145 
27      47      87      167 
28      45      79      147 
29      57      113     225 
30      52      96      184 
Answered By: Bibhav

How about this ?

list = [21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 39, 39, 44, 42, 40, 43, 47, 45, 57, 52, 75, 73, 86, 78, 70, 77, 87, 79, 113, 96, 147, 141, 170, 150, 130, 145, 167, 147, 225, 184]

x = np.array(list)              # Change list to 1D numpy matrix
x = x.reshape(int(len(x)/4), 4) # Reshape the matrix according to our requirement
Answered By: kgkmeekg

For the horizontal you can use np.reshape:

np.array(list).reshape(-1, 4)

and for the vertical you can transpose:

np.array(list).reshape(4, -1).T
Answered By: ML-Nielsen

You can do with range

Vertical

col = 4
vertical = [lst[i:i+col] for i in range(0,len(lst),col)]
for item in vertical:
    print(item)

Output:

[21, 22, 23, 24]
[25, 26, 27, 28]
[29, 30, 39, 39]
[44, 42, 40, 43]
[47, 45, 57, 52]
[75, 73, 86, 78]
[70, 77, 87, 79]
[113, 96, 147, 141]
[170, 150, 130, 145]
[167, 147, 225, 184]

Horizontal

row = 10
for s in range(row):
    print([lst[s:][i] for i in range(0,len(lst),row)])

Output:

[21, 39, 75, 147]
[22, 39, 73, 141]
[23, 44, 86, 170]
[24, 42, 78, 150]
[25, 40, 70, 130]
[26, 43, 77, 145]
[27, 47, 87, 167]
[28, 45, 79, 147]
[29, 57, 113, 225]
[30, 52, 96, 184]

Together into a function.

def printarray(row, col):

    print('----Vertical print----')
    vertical = [lst[i:i+col] for i in range(0,len(lst),col)]
    for item in vertical:
        print(item)

    print('n')

    print('----Horizontal print----')
    for s in range(row):
        print([lst[s:][i] for i in range(0,len(lst),row)])
Answered By: Rahul K P

If you use numpy library, it is very easy to reshape the list.

For making vertically reshaped array, you just make the array as 4 rows and 10 columns first, then transpose the array using numpy function.

import numpy as np
target_list = [21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 39, 39, 44, 42, 40, 43, 47, 45, 57, 52, 75, 73, 86, 78, 70, 77, 87, 79, 113, 96, 147, 141, 170, 150, 130, 145, 167, 147, 225, 184]

# for horizontally
h_array = np.array(target_list).reshape((10, 4))
print(h_array)

# for vertically
v_array = np.array(target_list).reshape((4, 10))
print(v_array.T)

If you want to make the array as list of list, then you can you tolist function.

h_list = h_array.tolist()
print(h_list)

v_list = v_array.T.tolist()
print(v_list)
Answered By: Suin SEO

Here are 2 functions that print both directions.

Horizontal output:

21      22      23      24      
25      26      27      28
29      30      39      39
44      42      40      43
47      45      57      52
75      73      86      78
70      77      87      79
113     96      147     141
170     150     130     145
167     147     225     184

Vertical output:

21      39      75      147     
22      39      73      141
23      44      86      170
24      42      78      150
25      40      70      130
26      43      77      145
27      47      87      167
28      45      79      147
29      57      113     225
30      52      96      184

Code:

data = [21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 39, 39, 44, 42, 40, 43, 47, 45, 57, 52, 75, 73, 86, 78, 70, 77, 87, 79, 113, 96, 147, 141, 170, 150, 130, 145, 167, 147, 225, 184]

def print_horizontaly(lst: list, columns, spacing):
    for i, item in enumerate(lst):
        if i % columns == 0:
            print("")

        print(item, end=''.join(' ' for j in range(spacing - len(str(item)))))
        

def print_verticaly(lst: list, columns, spacing):
    columns_ = []
    for i in range(len(lst)//columns):
        for item in lst[i::len(lst)//columns]:
            print(item, end=''.join(' ' for j in range(spacing - len(str(item)))))

        print("")


print_horizontaly(data, 4, 8)
print_verticaly(data, 4, 8)
Answered By: David Hožič