sarvai sarvai - 6 months ago 17
Python Question

how to read user defined input for 2d array in python

I have to read a n*n matrix where n and as well the elements of the matrix should be obtained from the user in the console. I understand that python sees a 2d array to be list in a list. I have read values for matrix in C and C++. But it seems different in python. I went through some examples and in all examples I was able to see only compile time input.How do we give user defined output from user.


As you already stated, you will have to use a list of lists.

main_list = []
for i in range(n):
  temp_list = []
  for j in range(n):
    temp_list.append(raw_input("Element {0}:{1}: ".format(i,j)))