How to create a 2d list from a input data?

You should know that ar is not defined when you are trying to perform an assignment like ar[i][j] = int(input()), there are many ways to fix that.

In C/C++

In C/C++, I presume you would do such work like this:

#include <cstdio>

int main()
{
    int m, n;
    scanf("%d %d", &m, &n);
    int **ar = new int*[m];
    for(int i = 0; i < m; i++)
        ar[i] = new int[n];
    for(int i = 0; i < m; i++)
        for(int j = 0; j < n; j++)
            scanf("%d", &ar[i][j]);
    // Do what you want to do
    
    for(int i = 0; i < m; i++)
        delete ar[i];
    delete ar;
   
    return 0;
}

Before you get inputs by scanf in C/C++, you should allocate storage by calling new or malloc, then you can perform your scanf, or it will crash.

How to do like that in Python

It's very similar to what you had done in C/C++, according to your code, when you are trying to perform assignment to ar[i][j], Python has no idea what ar it is! So you have to let it know first.

A NOT-pythonic way

A NOT-Pythonic way is do something like you did in C/C++:

n = int(input())
m = int(input())

ar = []
for i in range(m):
    ar.append([])
    for j in range(n):
        k = int(input())
        ar[i].append(k)

for i in range(m):
    for j in range(n):
        print(ar[i][j])

You initialize the list by ar = [] like you did int **ar = new int*[m]; in C/C++. For each row in the 2-d list, initialize the row by using ar.append([]) like you did ar[i] = new int[n]; in C/C++. Then, get your data by using input and append it to ar[i].

A pythonic way

The way to perform such a job like above it's not very pythonic, instead, you can get it done by using a feature called List Comprehensions, then the code can be simplified into this:

n = int(input())
m = int(input())

ar = [[0 for j in range(n)] for i in range(m)]
for i in range(m):
    for j in range(n):
        k = int(input())
        ar[i][j] = k

for i in range(m):
    for j in range(n):
        print(ar[i][j])

Note that the core ar = [[0 for j in range(n)] for i in range(m)] is a list comprehension that it creates a list which has m lists and for each list of these m lists it has n 0s.


You can initialize matrix in nested loop like this:

n = int(input()) # columns
m = int(input()) # rows
print(n,m)
matrix = []
for i in range(0,m):
    matrix.append([])
    for j in range(0,n):
        matrix[i].append(0)
        matrix[i][j] = int(input())
print matrix

You haven't declared ar yet. In Python, you don't have to perform separate declaration and initialization; nevertheless, you can't perform operations on names willy-nilly.

Start off with something like this:

ar = [[0 for j in range(m)] for i in range(n)]