Shivansh Kumar
Shivansh Kumar

Reputation: 361

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

I was trying this

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

But it was showing an error

Traceback (most recent call last):
  File "C:/Users/shivansh/Desktop/test.py", line 6, in <module>
    ar[i][j] = int(input())
NameError: name 'ar' is not defined

I used to do the same in C language but it works. So, how to this in Python?

Upvotes: 11

Views: 27233

Answers (7)

KM Minhaj
KM Minhaj

Reputation: 1

At Python-3 we can easily take row and column input side by side as well as the data like a matrix by this process.

  n, m = map(int, input("Enter row and column : ").split())    
  matrix = []      
  for i in range(0, n):
      matrix.append(list(map(int, input().strip().split()))[:m])

also i found it helpful.

Upvotes: 0

virmis_007
virmis_007

Reputation: 173

Since you haven't declared ar before, you cannot assign elements at specific indexes. You can decalre a two dimensional list in this manner initialized to a certain value.

arr = [[value]*c for _ in range(r)]

where r and c are desired number of rows and columns respectively

Upvotes: 1

Ankit Sharma
Ankit Sharma

Reputation: 1664

Actually, you don't need the column count. Just enter the number of rows and give your input space separated.

rows = int(input('Enter rows\n'))
my_list = []
for i in range(rows):
    my_list.append(list(map(int, input().split())))

Upvotes: 0

paras malhotra
paras malhotra

Reputation: 1

list=[]

n=int(input("enter no  of rows"))

m=int(input("enter no of columns"))

for i in range(0,m):
    list.append([])
for i in range(0,n):
   for j in range(0,m):
       list[i].append(j)
       list[i][j]=0
for i in range(0,n):
    for j in range(0,m):
        print("entry in row:",i+1,"entry in column:",j+1)
        list[i][j]=int(input())
print(list)

Upvotes: -1

Time1ess
Time1ess

Reputation: 109

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.

Upvotes: 2

manvi77
manvi77

Reputation: 570

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

Upvotes: 1

Arya McCarthy
Arya McCarthy

Reputation: 8814

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)]

Upvotes: 9

Related Questions