443,660 Members | 1,102 Online
Need help? Post your question and get tips & solutions from a community of 443,660 IT Pros & Developers. It's quick & easy.

# problem with two-dimensional list/array

 P: 20 hi, i've created a 2D list(ie: A=[]). i choose some of element from the list(ie: A[0][0],A[1][1],A[2][2]) and do simple add operations. Then i created another empty 2D list to store the result. but i wanted the result to be stored at a specific index (ie: at B=[0][0],B[1][1],B[2][2]). but i get an error."Error - list index out of range " here is my code...can anyone spot my mistake... Expand|Select|Wrap|Line Numbers A=[]         for i in range(3):             A.append([]) A[0]=(1,3,4,5) A[1]=(2,4,6,5) A[2]=(3,6,7,4)   B=[]       for j in range(3):              B.append([])              for i in range(3):                  B[i][i]=2+A[i][i]     Sep 2 '10 #1

Your indentation is wrong, but I suspect it is a copy/paste problem.

You did not initialize list B properly.
Expand|Select|Wrap|Line Numbers
1. A=[]
2. for i in range(3):
3.     A.append([])
4. A[0]=(1,3,4,5)
5. A[1]=(2,4,6,5)
6. A[2]=(3,6,7,4)
7.
8. B=[[None for i in range(4)] for j in range(3)]
9.
10. for i in range(3):
11.     B[i][i]=2+A[i][i]
Expand|Select|Wrap|Line Numbers
1. >>> B
2. [[3, None, None, None], [None, 6, None, None], [None, None, 9, None]]
3. >>>