I get the following error from running this code. Was wondering if 'i' and 'j' are considered objects.
If yes is there any way i could cast them..... How do i write a better for loop for this....

array2D = [[0 for i in range(10)]for j in range(10)]


for i in array2D:
for j in array2D[0]:
array2D[j] = 2;


Traceback (most recent call last):
File "/Users/yasinyaqoobi/Documents/workspace/Python_Hello_World/src/2dArray/__init__.py", line 7, in <module>
array2D[j] = 2;
TypeError: list indices must be integers

Take this

ad =[[x for x in range(1,10,2)],[w for w in range(2,20,3)]]

print(ad)
## out put
[[1, 3, 5, 7, 9], [2, 5, 8, 11, 14, 17]]

I do not know what you want to accomplish, but one guess:

array2D = [[0 for i in range(10)]for j in range(10)] 

for i in range(len(array2D)):
    array2D[i][0] = 2

print array2D

or

array2D = [[0 for i in range(10)]for j in range(10)] 

for row in array2D:
    row[0] = 2

print array2D

Diagonal matrix:

array2D = [[0 if i != j else 1 for i in range(10)]for j in range(10)] 

for row in array2D:
    print row

Edited 5 Years Ago by pyTony: n/a

This question has already been answered. Start a new discussion instead.