Transpose  

We can generate the transposition of an array using the tool numpy.transpose. 

It will not affect the original array, but it will create a new array.

import numpy

my_array = numpy.array([[1,2,3],
                        [4,5,6]])
print numpy.transpose(my_array)

#Output
[[1 4]
 [2 5]
 [3 6]]

Flatten

The tool flatten creates a copy of the input array flattened to one dimension.

import numpy

my_array = numpy.array([[1,2,3],
                        [4,5,6]])
print my_array.flatten()

#Output
[1 2 3 4 5 6]

Task  

You are given a $N$X$\mbox{M}$ integer array matrix with space separated elements ($N$ = rows and $\mbox{M}$ = columns). 

Your task is to print the transpose and flatten results.

Input Format

The first line contains the space separated values of $N$ and $\mbox{M}$. 

The next $N$ lines contains the space separated elements of $\mbox{M}$ columns.

Output Format

First, print the transpose array and then print the flatten.

Sample Input
2 2
1 2
3 4

Sample Output
[[1 3]
 [2 4]]
[1 2 3 4]

Difficulty: EASY

Solutions:
import numpy as np
inp = input()
(N, M) = map(int, inp.split())
matrix = np.empty((N, M), dtype=int)
for i in range(N):
	inp = input()
	matrix[i] = np.array(list(map(int, inp.split())))
print(matrix.T)
print(matrix.flatten())


import numpy
dim = input().rstrip().split(' ')
shape = tuple([int(x) for x in dim])
arr = []
for i in range(0, shape[0]):
	arr.append(numpy.array(input().split(' '), dtype=int))
arr1 = numpy.array(arr, dtype=int)
print(numpy.transpose(arr1))
print(arr1.flatten())


from numpy import array, transpose
shape = input()
ar = []
for _ in range(int(shape[0])):
	ar.append(input().split())
numpifiedArray = array(ar, dtype=int)
print(transpose(numpifiedArray))
print(numpifiedArray.flatten())


import numpy
(N, M) = map(int, input().split())
raw_a = []
for i in range(N):
	raw_a.append(list(map(int, input().split())))
print(numpy.transpose(numpy.array(raw_a)))
print(numpy.array(raw_a).flatten())


import numpy as np
a = []
(n, m) = tuple(map(int, input().split()))
for _ in range(n):
	a.append(list(map(int, input().split())))
b = np.array(a)
print(np.transpose(b))
print(b.ravel())


import numpy
(n, m) = map(int, input().split())
arrays = []
for i in range(n):
	arrays.append(list(map(int, input().split())))
my_array = numpy.array(arrays)
print(numpy.transpose(my_array))
print(my_array.flatten())


import numpy
(n, m) = map(int, input().split())
a = numpy.array([input().split() for _ in range(n)], int)
print(numpy.transpose(a))
print(a.flatten())


import numpy
(n, m) = map(int, input().split())
l = list()
for _ in range(n):
	l.append(list(map(int, input().split())))
a = numpy.array(l)
print(numpy.transpose(a))
print(a.flatten())


import numpy
a = numpy.array([numpy.array(input().split(), int) for _ in range(int(input().split()[0]))])
print(a.transpose(), a.flatten(), sep='\n')


import numpy
(N, M) = map(int, input().split())
a = numpy.array([], int)
for i in range(N):
	a = numpy.append(a, numpy.array([x for x in input().split()], int))
print(numpy.transpose(numpy.reshape(a, (N, M))))
print(a)


import numpy
(N, M) = map(int, input().split())
l = []
for _ in range(N):
	l.append(list(map(int, input().split())))
array = numpy.array(l)
print(numpy.transpose(array))
print(array.flatten())


import numpy
(n, m) = (int(i) for i in input().split())
l = []
for _ in range(n):
	l.append(input().split())
arr = numpy.array(l, int)
print(numpy.transpose(arr))
print(numpy.reshape(arr, n * m))


import numpy
a = []
inp = [int(x) for x in input().split()]
for i in range(inp[0]):
	a.append([int(x) for x in input().split()])
a = numpy.array(a)
print(numpy.transpose(a))
print(a.flatten())


import numpy
arr = numpy.array([], int)
(n, m) = map(int, input().split())
for _ in range(n):
	arr = numpy.append(arr, list(map(int, input().split())))
arr = numpy.reshape(arr, (n, m))
print(numpy.transpose(arr), arr.flatten(), sep='\n')


import numpy
(n, m) = [int(i) for i in input().strip().split()]
A = numpy.array([[int(j) for j in input().strip().split()] for i in range(n)])
print(numpy.transpose(A))
print(A.flatten())


import numpy as np
dim = input().split(' ')
dim = [int(x) for x in dim]
m = np.zeros(dim, int)
for j in range(0, dim[0]):
	arin = input().split(' ')
	ar = [int(x) for x in arin]
	m[j, :] = ar
print(np.transpose(m))
print(m.flatten())


import numpy
(N, M) = map(int, input().split())
myList = list()
for _ in range(N):
	myList.append(input().split())
Nlist = numpy.array(myList, int)
print(numpy.transpose(Nlist))
print(Nlist.flatten())


import numpy
(n, m) = map(int, input().split())
x = []
for i in range(n):
	x.extend(map(int, input().split()))
X = numpy.array(x)
X.shape = (n, m)
print(X.T)
print(X.flatten())


from numpy import *
(N, M) = [int(x) for x in input().split()]
L = []
for i in range(N):
	L.append([int(x) for x in input().split()])
A = array(L)
print(transpose(A))
print(A.flatten())


import numpy
inp = input()
(row, col) = inp.split()
arr = []
for r in range(int(row)):
	s = input()
	a = list(map(int, s.split()))
	arr.append(a)
my_nump = numpy.array(arr)
print(numpy.transpose(my_nump))
print(my_nump.flatten())


import numpy
(N, M) = map(int, input().split())
array = numpy.array([list(map(int, input().split())) for _ in range(N)])
print(array.transpose())
print(array.flatten())


import numpy
(n, m) = map(int, input().split())
lst = []
for _ in range(n):
	lst.append(list(input().split()))
array = numpy.array(lst, int)
print(numpy.transpose(array))
print(array.flatten())


import numpy
(N, M) = [int(x) for x in input().strip().split()]
tmp = []
for i in range(N):
	tmp.append([int(x) for x in input().strip().split()])
arr = numpy.array(tmp)
print(numpy.transpose(arr))
print(arr.flatten())


import numpy as np
(N, M) = map(int, input().split())
arr = np.array([list(map(int, input().split())) for _ in range(N)])
print(arr.T)
print(arr.flatten())


import numpy
(n, m) = map(int, input().split())
a = []
for _ in range(n):
	a.append(list(map(int, input().split())))
a = numpy.array(a)
print(numpy.transpose(a))
print(a.flatten())


import numpy
(N, M) = map(int, input().split())
a = numpy.array([input().split() for _ in range(N)], int)
print(numpy.transpose(a))
print(a.flatten())


import numpy as np
s = input()
(n, m) = s.split()
n = int(n)
m = int(m)
output = []
for i in range(0, n):
	tempString = input()
	tempArr = tempString.split()
	tempArr = list(map(int, tempArr))
	output.append(tempArr)
npa = np.array(output)
transposed = np.transpose(npa)
flat = npa.flatten()
print(transposed)
print(flat)


import numpy
(n, m) = list(map(int, input().split()))
ar = []
for i in range(n):
	row = list(map(int, input().split()))
	ar.append(row)
np_ar = numpy.array(ar)
print(numpy.transpose(np_ar))
print(np_ar.flatten())


import numpy
(N, M) = map(int, input().split())
elements = list()
for n in range(N):
	elements.append(input().split())
matrix = numpy.reshape(numpy.array(elements, int), (N, M))
print(numpy.transpose(matrix))
print(matrix.flatten())


import numpy
l = []
(N, M) = map(int, input().strip().split())
for k in range(N):
	l.append(list(map(int, input().strip().split())))
arr = numpy.array(l, int)
print(arr.transpose())
print(arr.flatten())


import numpy
import operator

def read_f(f):
	return f(input())

def read_f_words(f):
	return map(f, input().split())

def mapl(f, x):
	return list(map(f, list(x)))
(n, m) = read_f_words(int)
ns = []
for x in range(n):
	ns += read_f_words(int)
a = numpy.array(ns)
a = numpy.reshape(a, (n, m))
print(numpy.transpose(a))
print(a.flatten())


import numpy
(N, M) = list(map(int, input().split(' ')))
array = []
for i in range(N):
	row = list(map(int, input().split(' ')))
	for j in range(len(row)):
		array.append(row[j])
array = numpy.array(array)
array.shape = (N, M)
print(numpy.transpose(array))
print(array.flatten())


import numpy
(N, M) = map(int, input().split())
lis = [[int(i) for i in input().split()] for row in range(N)]
mylis = numpy.array(lis)
print(numpy.transpose(mylis))
print(mylis.flatten())


import numpy
(N, M) = [int(e) for e in input().split(' ')]
L = []
for a0 in range(N):
	L.append([int(e) for e in input().split(' ')])
my_array = numpy.array(L)
print(numpy.transpose(my_array))
print(my_array.flatten())


import numpy
(n, m) = map(int, input().split())
a = []
for _ in range(n):
	a.append(input().split())
a = numpy.reshape(numpy.array(a, int), (n, m))
print(numpy.transpose(a))
print(a.flatten())


import numpy as np

def main():
	(n, m) = map(int, input().split())
	l = []
	for _ in range(n):
		l.append(input().split())
	l = np.array(l, int)
	print(l.transpose())
	print(l.flatten())
main()


import numpy as np
(n, m) = map(int, input().split())
x = np.array([], dtype=int)
for _ in range(n):
	x = np.append(x, np.fromstring(input(), dtype=int, sep=' '))
x = x.reshape(n, m)
print(x.T)
print(x.flatten())


import numpy
(n, m) = map(int, input().split())
mat = numpy.array([[int(x) for x in input().split()] for k in range(n)])
print(numpy.transpose(mat))
print(mat.flatten())


import numpy
l = []
(m, n) = map(int, input().split())
for i in range(m):
	l.append(input().split())
a = numpy.array(l, int)
print(numpy.transpose(a))
print(numpy.array(l, int).flatten())


import numpy as np
import sys
(N, M) = list(map(int, sys.stdin.readline().split()))
A = np.array([sys.stdin.readline().split() for _ in range(N)], int)
print(A.transpose())
print(A.flatten())


import numpy as np
(N, M) = map(int, input().split())
A = np.array([list(map(int, input().split())) for _ in range(N)])
print(np.transpose(A))
print(A.flatten())


import numpy as np
(n, m) = [int(i) for i in input().split()]
l = []
for _ in range(n):
	l.append([int(i) for i in input().split()])
L = np.array(l)
print(np.transpose(L))
print(L.flatten())


import numpy
(N, M) = map(int, input().split())
a = numpy.array([list(map(int, input().split())) for _ in range(N)], int)
print(a.transpose())
print(a.flatten())


import numpy
(n, m) = [int(x) for x in input().split(' ')]
dat = []
for _ in range(n):
	dat.append([int(x) for x in input().split(' ')])
arr = numpy.array(dat)
print(arr.transpose())
print(arr.flatten())


import numpy
(n, m) = map(int, input().split())
l = numpy.array([input().split() for _ in range(n)], int)
print(numpy.transpose(l), l.flatten(), sep='\n')


import numpy
(n, m) = map(int, input().split())
a = []
for i in range(n):
	a.append(list(map(int, input().split())))
b = numpy.array(a)
print(b.transpose())
print(b.flatten())


import numpy as np
(N, M) = map(int, input().split(' '))
i = np.array([input().split(' ') for i in range(N)], int)
print(i.transpose())
print(i.flatten())


import numpy
a = numpy.array(input().split(), int)
b = []
for i in range(a[0]):
	b.append(list(map(int, input().split())))
b = numpy.array(b)
print(numpy.transpose(b))
print(b.flatten())


import numpy as np
(n, m) = map(int, input().split())
listo = []
for i in range(n):
	listo.append(list(map(int, input().split())))
arro = np.array(listo)
arro.shape = (n, m)
print(np.transpose(arro))
print(arro.flatten())


import numpy
input()
l = list()
while True:
	try:
		l.append(input().split())
	except EOFError:
		break
print(numpy.transpose(numpy.array(l, int)))
print(numpy.array(l, int).flatten())


import numpy
(n, m) = [int(n) for n in input().split()]
arr = []
for _ in range(n):
	arr.append([int(n) for n in input().split()])
nparr = numpy.array(arr)
print(numpy.transpose(nparr))
print(nparr.flatten())


import numpy
metric = list(map(int, input().split()))
n = metric[0]
m = metric[1]
l = []
for i in range(n):
	l.append(list(map(int, input().split())))
a = numpy.array(l)
print(a.transpose())
print(a.flatten())


import numpy
(n, m) = map(int, input().split())
data = numpy.reshape(numpy.array(' '.join((input() for _ in range(n))).split(), int), (n, m))
print(numpy.transpose(data))
print(data.flatten())


import numpy as np
(m, n) = map(int, input().strip().split(' '))
arr = []
for i in range(m):
	arr.append(np.array(list(map(int, input().strip().split(' ')))))
arr = np.array(arr)
print(arr.transpose())
print(arr.flatten())


import numpy
a = []
(n, m) = map(int, input().strip().split())
for i in range(n):
	a.append(list(map(int, input().strip().split())))
b = numpy.array(a)
print(numpy.transpose(b))
print(b.flatten())


import numpy
(N, M) = map(int, input().split())
alist = []
for i in range(0, N):
	alist.extend(list(map(int, input().split())))
a = numpy.array(alist)
a.shape = (N, M)
print(a.transpose())
print(a.flatten())


import numpy
(N, M) = input().split()
N = int(N)
M = int(M)
arr = numpy.array([[int(x) for x in input().split()] for _ in range(N)])
print(arr.transpose())
print(arr.flatten())


import numpy
a = list(map(int, input().split()))
b = []
for i in range(a[0]):
	b = b + list(map(int, input().split()))
b = numpy.array(b)
b = numpy.reshape(b, (a[0], a[1]))
print(numpy.transpose(b))
print(b.flatten())


import numpy
(n, m) = map(int, input().split())
l = []
for _ in range(n):
	l.append(list(map(int, input().split())))
l = numpy.array(l)
print(numpy.transpose(l))
print(l.flatten())


import numpy
arr_shape = tuple(map(int, input().split()))
arr = []
for i in range(arr_shape[0]):
	arr.extend(list(map(int, input().split())))
arr = numpy.reshape(arr, arr_shape)
print(numpy.transpose(arr))
print(arr.flatten())


import numpy
(N, M) = tuple(map(int, input().split()))
iArr = [list(map(int, input().split())) for i in range(N)]
nArr = numpy.array(iArr)
print(numpy.transpose(nArr))
print(nArr.flatten())


import numpy
(N, M) = map(int, input().split())
A = numpy.array([list(map(int, input().split())) for _ in range(N)])
print(A.transpose(), A.flatten(), sep='\n')


import numpy
(n, m) = map(int, input().split())
inp = []
for i in range(n):
	inp.extend(map(int, input().split()))
a = numpy.array(inp)
print(numpy.transpose(numpy.reshape(a, (n, m))))
print(a)


import numpy
(n, m) = [int(x) for x in input().strip().split()]
arr = []
for _ in range(n):
	arr.append([int(x) for x in input().strip().split()])
print(numpy.array(arr).transpose())
print(numpy.array(arr).flatten())


import numpy
a = []
for _ in range(int(input().strip().split()[0])):
	a.append(list(map(int, input().strip().split())))
a = numpy.array(a)
print(a.transpose())
print(a.flatten())


import numpy
list = input().split()
list1 = []
for i in range(int(list[0])):
	list1.append(input().split())
	list1[i] = [int(x) for x in list1[i]]
a = numpy.array(list1)
b = numpy.transpose(a)
print(b)
a = a.flatten()
print(a)


import numpy as np
(N, M) = map(int, input().split())
arr = [input().split() for i in range(N)]
nparr = np.array(arr, int)
nparr.shape = (N, M)
print(nparr.transpose())
print(nparr.flatten())


import numpy
(n, m) = map(int, input().split())
arr = numpy.array([list(map(int, input().split())) for i in range(n)])
print(numpy.transpose(arr))
print(arr.flatten())


import numpy
(N, M) = [int(x) for x in input().split()]
ll = []
for l in range(N):
	ll += [int(x) for x in input().split()]
a = numpy.array(ll)
print(numpy.transpose(a.reshape(N, M)))
print(a)


import numpy
(n, m) = [int(x) for x in input().split()]
my_array = []
for _ in range(n):
	my_array.append([int(x) for x in input().split()])
print(numpy.transpose(numpy.array(my_array)))
print(numpy.array(my_array).flatten())


import numpy
(k, m) = map(int, input().split(' '))
temp = ''
for i in range(k):
	temp += input() + ' '
temp = temp[:-1]
arr = numpy.array(temp.split(' '), int)
print(numpy.transpose(numpy.reshape(arr, (k, m))))
print(arr)


import numpy as np
L = []
[N, M] = list(map(int, input().split()))
for i in range(N):
	L.append(list(map(int, input().split())))
npArr = np.array(L)
print(np.transpose(npArr))
print(npArr.flatten())


import numpy
(n, m) = list(map(int, input().split(' ')))
matrix = [input().split(' ') for i in range(n)]
data = numpy.array(matrix, int)
print(numpy.transpose(data))
print(data.flatten())


import numpy
(n, m) = map(int, input().split())
a = numpy.array([list(map(int, input().split())) for _ in range(n)])
print(numpy.transpose(a))
print(a.flatten())


import numpy
(N, M) = map(int, input().split())
my_array = numpy.array([input().split() for i in range(N)], int)
print(numpy.transpose(my_array))
print(my_array.flatten())


import numpy
(n, m) = map(int, input().strip().split())
a = numpy.array([list(map(int, input().strip().split())) for _ in range(n)])
print(numpy.transpose(a))
print(a.flatten())


import numpy
matrix1 = []
(N, M) = list(map(int, input().strip().split()))
for i in range(N):
	matrix1.append(list(map(int, input().strip().split())))
print(numpy.transpose(matrix1))
print(numpy.array(matrix1).flatten())


import numpy
(n, m) = map(int, input().split())
lista = [list(map(int, input().split())) for i in range(n)]
a = numpy.array(lista)
print(numpy.transpose(a))
print(a.flatten())


import numpy
(N, M) = input().split()
my_arr = []
for row in range(int(N)):
	my_arr.append(input().split())
print(numpy.transpose(numpy.array(my_arr, int)))
print(numpy.array(my_arr, int).flatten())


import numpy
(N, M) = input().split()
a = []
for _ in range(int(N)):
	a.append(input().split())
A = numpy.array(a, int)
print(A.transpose())
print(A.flatten())


import numpy
(n, m) = map(int, input().split())
for i in range(0, n):
	if i == 0:
		lis = numpy.array(list(input().split()), int)
	else:
		tlis = numpy.array(list(input().split()), int)
		lis = numpy.vstack((lis, tlis))
print(numpy.transpose(lis))
print(lis.flatten())


import numpy as np
(n, m) = [int(i) for i in input().split()]
a = np.zeros((n, m), dtype='int64')
for i in range(n):
	a[i] = [int(i) for i in input().split()]
print(a.T)
print(a.flatten())


import numpy as np

def parse_input(arg=None):
	raw = input() if arg is None else arg
	(n, m) = [int(i) for i in raw.split(' ')]
	data = [input() for e in range(n)]
	data = [[int(i) for i in e.split(' ')] for e in data]
	return (n, m, data)
(n, m, data) = parse_input()
x = np.array(data)
print(np.transpose(x))
print(x.flatten())


import numpy
a = []
(N, M) = map(int, input().split())
for i in range(N):
	a += list(map(int, input().split()))
a = numpy.array(a)
a.shape = (N, M)
print(numpy.transpose(a))
print(a.flatten())


import numpy
(n, m) = map(int, input().split())
a = []
for i in range(n):
	a.append(list(map(int, input().split())))
a = numpy.array(a)
print(numpy.transpose(a))
print(a.flatten())


from numpy import array, transpose
a = array([input().split() for _ in range(int(input().split()[0]))], int)
print(transpose(a), a.flatten(), sep='\n')


import numpy
(N, M) = map(int, input().split())
L = list()
for _ in range(N):
	L.extend(input().split())
A = numpy.array(L, int)
A.shape = (N, M)
print(numpy.transpose(A))
print(A.flatten())


import numpy
(N, M) = map(int, input().split())
arr = []
for i in range(0, N):
	arr.append([int(x) for x in input().split()])
my_arr = numpy.array(arr)
t = numpy.transpose(my_arr)
print(t)
print(my_arr.flatten())


import numpy
(N, M) = input().split()
r = []
for i in range(int(N)):
	r.append([i for i in map(int, input().split())])
my_array = numpy.array(r)
print(my_array.transpose())
print(my_array.flatten())


import numpy
l = []
for _ in range(int(input().split()[0])):
	c = input().split()
	l.append(c)
ar = numpy.array(l, int)
print(numpy.transpose(ar))
print(ar.flatten())


import numpy
(n, m) = map(int, input().split())
l = []
for i in range(n):
	l.append(list(map(int, input().split())))
l = numpy.array(l)
print(l.transpose())
print(l.flatten())


import numpy
(n, m) = [int(x) for x in input().split()]
x = numpy.array([[int(x) for x in input().split()] for _ in range(n)])
print(x.transpose())
print(x.flatten())


import numpy
nums = input()
lisin = nums.split()
n = int(lisin[0])
m = int(lisin[1])
newlis = []
for i in range(n):
	newlis.append(input().split())
a = numpy.array(newlis, int)
a.shape = (n, m)
print(numpy.transpose(a))
print(a.flatten())


import numpy
(n, m) = [int(a) for a in input().split(' ')]
arr = []
for a in range(n):
	arr.append([int(b) for b in input().split(' ')])
arr = numpy.array(arr)
print(numpy.transpose(arr))
print(arr.flatten())


import numpy
(n, m) = map(int, input().split())
input_arrays = numpy.zeros([n, m], int)
for rank in range(n):
	input_arrays[rank] = numpy.array(input().split(), int)
print(numpy.transpose(input_arrays))
print(input_arrays.flatten())


import numpy as np
(r, c) = map(int, input().split())
ls = []
for i in range(r):
	ls.append([int(x) for x in input().split()])
mat = np.array(ls)
print(mat.transpose())
print(mat.flatten())


import numpy
(N, M) = map(int, input().split())
a = numpy.array([list(map(int, input().split())) for _ in range(N)])
print(a.transpose())
print(a.flatten())


import numpy
a = numpy.array([[int(x) for x in input().split()] for _ in range(int(input().split()[0]))])
print(numpy.transpose(a))
print(a.flatten())


import numpy
(N, M) = map(int, input().split())
NP = numpy.array([list(map(int, input().split())) for i in range(N)])
print(NP.transpose())
print(NP.flatten())


import numpy
x = list(map(int, input().split()))
l = []
for i in range(x[0]):
	a = list(map(int, input().split()))
	l.append(a)
my_array = numpy.array(l)
print(numpy.transpose(my_array))
print(my_array.flatten())