python reverse 2d list

89

In [53]: l = [0,1,2,3]

In [54]: def to_matrix(l, n):
    ...:     return [l[i:i+n] for i in xrange(0, len(l), n)]

In [55]: to_matrix(l,2)
Out[55]: [[0, 1], [2, 3]]
>>> a = [[6, 5, 4], [3, 2, 1]]				# 2d array
>>> print([i[::-1] for i in a[::-1]])		# reverse each element in the arrays and the arrays themselves		
# output: [(1, 2, 3), (4, 5, 6)]

Comments

Submit
0 Comments