Matrix input in N*N form in Python I am using the below code in Python to input a N*N matrix in its original form:

n = int(input())
l=[]
for i in range(n):
    x=input().split()
    c=[]
    for j in range(n):
        c.append(int(x[j]))
    l.append(c)

print(l)


Here's the output:

2 #input n

1 3 #input row

1 5. # input another row

[[1, 3], [1, 5]] # final matrix


How can this code be improved? Is there a better way to take a matrix as input?
Since the number of rows is to be equal to the number of columns, you can simply get the number of columns from the first input and stop taking input when the user enters the same number of rows:

l = []
while not l or len(l) &lt; len(l[0]):
    l.append(list(map(int, input().split())))


so that given an input of:

2 3 4
1 2 3
5 6 7


l would become:

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