sarvai sarvai - 2 years ago 101
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.

Answer Source

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)))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download