2025, Dec 23 19:00
Reading a 2D Matrix from input() in Python: parse list literals with ast or row/col text with NumPy
Learn how to read a 2D matrix from input() in Python: parse list-of-lists with ast.literal_eval or accept row/col text for NumPy. Avoid list(input()) pitfalls.
Reading a 2D matrix from input() in Python is tricky if you expect a ready-to-use list of lists. input() always returns a string, so without parsing, you’ll end up with text instead of a real structure you can compute on.
Problem overview
The goal is to let a user enter something like [[1,2],[3,4]] and obtain a proper 2D list. A straightforward attempt only echoes a string and doesn’t give you a usable matrix.
matrix_text = input([[1,2],[3,4]])
print(matrix_text)
What you see printed looks like a matrix, but it’s still just text. Another wrong turn is trying list(input()), which only splits the string into individual characters and destroys the structure entirely.
What’s actually happening
input() returns raw text. To compute with it, you must convert that text into a Python object. For nested structures such as a list of lists, character-by-character splitting won’t work; you need a parser that understands literals like brackets, commas, and numbers. That’s exactly what the methods below provide.
Solution 1: Parse a Python-like literal with ast
When the user types a Python-style list literal (for example, [[1,2],[3,4]]), you can parse it into a real list using ast. This turns the input string into a proper list of lists that you can index, iterate, and modify.
import ast
user_line = input("Enter the matrix (e.g., [[1,2],[3,4]]): ")
parsed_matrix = ast.literal_eval(user_line)
After this, parsed_matrix is a normal Python 2D list. You can pass it to functions, sum rows, or do whatever processing you need.
Solution 2: Read a compact matrix format with numpy
If you’re fine with a different input syntax, you can accept rows separated by semicolons and values separated by commas, such as 1,2;3,4, and feed it to numpy. This requires a slightly different input format but yields a matrix immediately.
import numpy as np
text_buf = input("Enter the matrix (e.g., 1,2;3,4): ")
arr2d = np.matrix(text_buf, dtype=int)
Here the parsing expectations are different: commas separate columns and semicolons separate rows. The result is a matrix object constructed from the provided string.
Why this matters
Matrix-shaped input shows up in quick prototypes, coding interviews, scripts, and small tools. Treating the data as text makes every downstream step harder. Converting it into a real structure early keeps your code predictable and your operations intuitive. It also minimizes manual string handling and the subtle bugs that come with it.
Takeaways
If the input looks like a Python literal, parse it as such to get a real list of lists. If you prefer a compact numeric format, accept a row/column syntax and let numpy interpret it. Avoid character-level transformations like list(input()) when dealing with nested structures; they destroy the shape you actually need. Choose one format, make it explicit in the prompt, and parse it consistently.