Python Questions and Answers – Matrix List Comprehension

This set of Python Multiple Choice Questions & Answers (MCQs) focuses on “Matrix List Comprehension”.

1. Which of the following matrices will throw an error in Python?
a)

        A = [[1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]]

b)

        B = [[3, 3, 3]
        [4, 4, 4]
        [5, 5, 5]]

c)

        C = [(1, 2, 4),
        (5, 6, 7),
        (8, 9, 10)]
advertisement

d)

        D = [2, 3, 4,
        3, 3, 3,
        4, 5, 6]
View Answer
Answer: b
Explanation: In matrix B will result in an error because in the absence of a comma at the end of each row, it behaves like three separate lists. The error thrown states that the list integers must be integers or slices, not tuples.
 
 

2. What will be the output of the following Python code?

Free 30-Day C Certification Bootcamp is Live. Join Now!
A = [[1, 2, 3],
          [4, 5, 6],
          [7, 8, 9]]
A[1]

a) [4, 5, 6]
b) [3, 6, 9]
c) [1, 4, 7]
d) [1, 2, 3]
View Answer

Answer: a
Explanation: In the given 2D list A, A[1] refers to the second row (indexing starts from 0). So, A[1] retrieves the list [4, 5, 6], which is the second row of the matrix.

3. Which of the following Python statements will result in the output: 6?

A = [[1, 2, 3],
     [4, 5, 6],
     [7, 8, 9]]

a) A[2][3]
b) A[2][1]
c) A[1][2]
d) A[3][2]
View Answer

Answer: c
Explanation: In the 2D list A, the second row (index 1) is [4, 5, 6]. The third element in this row (index 2) is 6. Therefore, A[1][2] results in the output 6.
advertisement

4. What will be the output of the following Python code?

A = [[1, 2, 3],
      [4, 5, 6],
      [7, 8, 9]]
[A[row][1] for row in (0, 1, 2)]

a) [7, 8, 9]
b) [4, 5, 6]
c) [2, 5, 8]
d) [1, 4, 7]
View Answer

Answer: c
Explanation: The list comprehension [A[row][1] for row in (0, 1, 2)] iterates over each row and selects the element at index 1 (the second element) from each row. The second element in each row is 2, 5, and 8, producing the output [2, 5, 8].

5. What will be the output of the following Python code?

A = [[1, 2, 3],
     [4, 5, 6],
     [7, 8, 9]]
[A[i][i] for i in range(len(A))]

a) [1, 5, 9]
b) [3, 5, 7]
c) [4, 5, 6]
d) [2, 5, 8]
View Answer

Answer: a
Explanation: The list comprehension [A[i][i] for i in range(len(A))] selects elements where the row index and column index are the same, effectively pulling out the diagonal of the matrix. This results in the elements A[0][0], A[1][1], and A[2][2], which are 1, 5, and 9, respectively.

6. What will be the output of the following Python code?

l=[[1, 2, 3], [4, 5, 6]]
for i in range(len(l)):
	for j in range(len(l[i])):
		l[i][j]+=10
l

a) No output
b) Error
c) [[1, 2, 3], [4, 5, 6]]
d) [[11, 12, 13], [14, 15, 16]]
View Answer

Answer: d
Explanation: The code iterates through each sublist in l, and within each sublist, it adds 10 to each element. After the loop, the updated list is [[11, 12, 13], [14, 15, 16]].

7. What will be the output of the following Python code?

A = [[1, 2, 3],
     [4, 5, 6],
     [7, 8, 9]]
 
[[col + 10 for col in row] for row in A]

a) [[11, 12, 13], [14, 15, 16], [17, 18, 19]]
b) Error
c) [11, 12, 13], [14, 15, 16], [17, 18, 19]
d) [11, 12, 13, 14, 15, 16, 17, 18, 19]
View Answer

Answer: a
Explanation: The list comprehension [[col + 10 for col in row] for row in A] iterates over each row in matrix A and adds 10 to each element in that row. The resulting matrix is [[11, 12, 13], [14, 15, 16], [17, 18, 19]].

8. What will be the output of the following Python code?

A = [[1, 2, 3],
     [4, 5, 6],
     [7, 8, 9]]
[A[i][len(A)-1-i] for i in range(len(A))]

a) [1, 5, 9]
b) [4, 5, 6]
c) [3, 5, 7]
d) [2, 5, 8]
View Answer

Answer: c
Explanation: The expression [A[i][len(A)-1-i] for i in range(len(A))] selects elements from the anti-diagonal of the square matrix A. It fetches A[0][2], A[1][1], and A[2][0], which are 3, 5, and 7, resulting in [3, 5, 7].

9. What will be the output of the following Python code?

A = [[1, 2, 3],
     [4, 5, 6],
     [7, 8, 9]]
B = [[3, 3, 3],
     [4, 4, 4],
     [5, 5, 5]]
[B[row][col]*A[row][col] for row in range(3) for col in range(3)]

a) [3, 6, 9, 16, 20, 24, 35, 40, 45]
b) Error
c) [0, 30, 60, 120, 160, 200, 300, 350, 400]
d) 0
View Answer

Answer: a
Explanation: The list comprehension [B[row][col]*A[row][col] for row in range(3) for col in range(3)] performs element-wise multiplication of two 3×3 matrices A and B, row by row. The results are flattened into a single list:
[1×3, 2×3, 3×3, 4×4, 5×4, 6×4, 7×5, 8×5, 9×5] → [3, 6, 9, 16, 20, 24, 35, 40, 45].

10. What will be the output of the following Python code?

r = [11, 12, 13, 14, 15, 16, 17, 18, 19]
A = [[0, 10, 20],
               [30, 40, 50],
               [60, 70, 80]]
for row in A:
	for col in row:
		r.append(col+10)
r

a) [11, 12, 13, 14, 15, 16, 17, 18, 19, 10, 20, 30, 40, 50, 60, 70, 80, 90]
b) [10, 20, 30, 40, 50, 60, 70, 80, 90]
c) [11, 12, 13, 14, 15, 16, 17, 18, 19]
d) [0, 10, 20, 30, 40, 50, 60, 70, 80]
View Answer

Answer: a
Explanation: The original list r starts with values [11, 12, …, 19]. The nested loop iterates over all elements of the 2D list A, adding 10 to each and appending the result to r.
New values appended: [10, 20, 30, 40, 50, 60, 70, 80, 90].

11. What will be the output of the following Python code?

A = [[1, 2, 3],
     [4, 5, 6],
     [7, 8, 9]]
B = [[3, 3, 3],
     [4, 4, 4],
     [5, 5, 5]]
[[col1 * col2 for (col1, col2) in zip(row1, row2)] for (row1, row2) in zip(A, B)]

a) [0, 30, 60, 120, 160, 200, 300, 350, 400]
b) [[3, 6, 9], [16, 20, 24], [35, 40, 45]]
c) No output
d) Error
View Answer

Answer: b
Explanation: The code multiplies corresponding elements of matrices A and B using nested list comprehensions with zip(). Each row from A and B is paired, and their elements are multiplied element-wise. The result is a new matrix with the same dimensions.

12. What will be the output of the following Python code?

A = [[1, 2, 3],
     [4, 5, 6],
     [7, 8, 9]]
B = [[3, 3, 3],
     [4, 4, 4],
     [5, 5, 5]]
zip(A, B)

a) Address of the zip object
b) Address of the matrices A and B
c) No output
d) [3, 6, 9, 16, 20, 24, 35, 40, 45]
View Answer

Answer: a
Explanation: The function zip(A, B) returns a zip object, which is an iterator of paired rows from A and B. Since it’s not converted to a list or looped over, printing it will show its memory address.

>>> list(zip(A, B))
[([1, 2, 3], [3, 3, 3]), ([4, 5, 6], [4, 4, 4]), ([7, 8, 9], [5, 5, 5])]

Sanfoundry Global Education & Learning Series – Python.

To practice all areas of Python, here is complete set of 1000+ Multiple Choice Questions and Answers.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.