2

I need to take an n by n array and transpose it in Python-3. The example given is that I have this list input M:

[[4, 2, 1], ["a", "a", "a"], [-1, -2, -3]]

and it needs to be transposed to read:

[[4, 'a', -1], [2, 'a', -2], [1, 'a', -3]]

So basically reading vertically instead of horizontally.

I CANNOT use things like zip or numpy, I have to make my own function, trans(M).

If anyone could help and provide an explanation so I can learn it, I'd be grateful.

5
  • Hint: Wikipedia says, "the ith row, jth column element of A_transposed is the jth row, ith column element of A." Commented Apr 30, 2014 at 16:33
  • use the numpy lib. something like this np.array([5,4])[np.newaxis] and print the result print a.T Commented Apr 30, 2014 at 16:35
  • 1
    Does this answer your question? Transpose list of lists Commented Nov 16, 2022 at 12:46
  • Duplicate: Transpose list of lists. It doesn't stipulate "no zip or numpy", but there's at least one answer (matchew's) that meets that requirement. Commented Apr 16 at 18:50
  • 1
    Er, what do you mean by "things like zip or numpy"? E.g. are you not allowed to use any builtins (like zip), meaning range is off the table too? You can edit to clarify. And please mention what research you've done, e.g. if you already saw that question but matchew's answer doesn't meet your requirement because it uses range, please tell us that to save us having to rehash it. For more tips, see How to Ask. [I know this is an old question, but I'm just giving the same advice I would to a new question.] Commented Apr 16 at 18:54

4 Answers 4

4

A one-liner:

def trans(M):
    return [[M[j][i] for j in range(len(M))] for i in range(len(M[0]))]

result:

>>> M = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> trans(M)
[[1, 4, 7], [2, 5, 8], [3, 6, 9]
# or for a non-square matrix:
>>> N = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
>>> trans(N)
[[1, 4, 7, 10], [2, 5, 8, 11], [3, 6, 9, 12]]

Additional Note: If you look up the tutorial on list comprehension, one of the examples is in fact transposition of a matrix array.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you to both you and @Morten Zilmer. I'm not quite sure I understand why it works though. How does the "for i in range(len(M[0]))] part work?
The assumption is that all rows (inner lists) have same length, which is the number of columns given by len(M[0]). The range(len(M[0])) iterates over the columns, and creates rows in the transposed matrix based on all row elements in each column of M.
@UncleChaos - it's called list comprehension (explained here - the explanation and examples in the tutorial should make it clear how it works)
1

A variant that should work for matrices with irregular row lengths:

m = [[3, 2, 1],
     [0, 1],
     [2, 1, 0]]

maxlen = max(len(row) for row in m)
m_T = [[row[c] for row in m if c < len(row)] for c in range(0, maxlen)]

m_T:

[[3, 0, 2],
 [2, 1, 1],
 [1, 0]]

Comments

0

Here is an in place solution that works for square matrices:

def trans(M):
  n = len(M)
  for i in range(n - 1):
    for j in range(i + 1, n):
      M[i][j], M[j][i] = M[j][i], M[i][j]

Example Usage:

def print_matrix(M):
  for row in M:
    for ele in row:
      print(ele, end='\t')
    print()
  
M = [[4, 2, 1], ["a", "a", "a"], [-1, -2, -3]]
print('Original Matrix:')
print_matrix(M)
trans(M)
print('Transposed Matrix:')
print_matrix(M)

Output:

Original Matrix:
4   2   1
a   a   a
-1  -2  -3
Transposed Matrix:
4   a   -1
2   a   -2
1   a   -3

Comments

0
y = ([1,2], [3,4], [5,6])

transpose = [[row[i] for row in y] for i in range(len(y[0]))]

the output is

[[1, 3, 5], [2, 4, 6]]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.