'Print list of lists into matrix format

I have printed the elements of a list of lists like this:

['1', 'A']
['1', 'B']
['2', 'A']
['2', 'B']
['3', 'A']
['3', 'B']

I am wondering how can I print that elements like this (a 3*2 matrix in this case):

['1', 'A']  ['1', 'B']
['2', 'A']  ['2', 'B']
['3', 'A']  ['3', 'B']

The number of rows is the number of different numbers and the number of columns is the number of different letters.

For instance, if I have:

['1', 'A']
['1', 'B']

The output would be displayed in 1 row and 2 columns:

['1', 'A']  ['1', 'B']

At this moment I have tried converting the list of lists into a numpy array and try to print the desired output but it does not work:

lst = [['1', 'A'],['1', 'B'],['2', 'A'],['2', 'B'],['3', 'A'],['3', 'B']]
arr = numpy.array(lst)
arr = arr.reshape(3,2)
print(*arr) 

Examples:

Input:

[['1', 'A'],['1', 'B'] ,['1', 'C'], ['1', 'D']]

Output:

['1', 'A']['1', 'B'] ['1', 'C'] ['1', 'D']

Input:

[['1', 'A'],['1', 'B'] ,['1', 'C'], ['1', 'D'],['2', 'A'],['2', 'B'] ,['2', 'C'], ['1', 'D']]

Output:

['1', 'A']['1', 'B'] ['1', 'C'] ['1', 'D']
['2', 'A']['2', 'B'] ['2', 'C'] ['2', 'D']

Input:

[['1', 'A'],['1', 'B'],['2', 'A'],['2', 'B'],['3', 'A'],['3', 'B']]

Output:

['1', 'A']['1', 'B']  
['2', 'A']['2', 'B']
['3', 'A']['3', 'B']


Solution 1:[1]

Here is a python only version

tmp = list(map(str, lst))
s = '\n'.join([' ' .join((l1, l2)) for l1, l2 in zip(tmp[::2], tmp[1::2])])
print(s)

output:
['1', 'A'] ['1', 'B']
['2', 'A'] ['2', 'B']
['3', 'A'] ['3', 'B']

Solution 2:[2]

For your specific list, you probably want to reshape it to (3, 2, 2) - i.e. n_rows * n_cols * n_elements_in_sublist

lst = [['1', 'A'],['1', 'B'],['2', 'A'],['2', 'B'],['3', 'A'],['3', 'B']]
arr = numpy.array(lst).reshape(3, 2, 2)

You can extract the n_rows and n_cols from list as

n_rows = len(set(x for x,y in lst))
n_cols = len(set(y for x,y in lst))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tom McLean
Solution 2 Mortz