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

Thanks guys... That helped....

Well you can close the thread as solved.
You are welcome
;)

The fastest way to make a 2D array of a constant value is

array2D= [[0]*numCols]*numRows
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.