LearnFrenzy
Sign in Take a free test

List Comprehensions · Predict the Output · Question 6

Transposing with a nested comprehension Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 6 Back to Exercise

What is the output of the following Python program?

main.py
m = [[1, 2, 3], [4, 5, 6]]
print([[row[i] for row in m] for i in range(3)])
  • A [[1, 2, 3], [4, 5, 6]]
  • B [[1, 2], [3, 4], [5, 6]]
  • C [[1, 4], [2, 5], [3, 6]] Correct
  • D [1, 4, 2, 5, 3, 6]
Correct Answer: Option C

For each column index i, the inner comprehension collects row[i] from every row.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links