Skip to content Skip to sidebar Skip to footer

Iterate Across Columns In A List Of Lists In Python

When I attempt iteration across columns in a row, the column does no change within a nested loop: i_rows = 4 i_cols = 3 matrix = [[0 for c in xrange(i_cols)] for r in xrange(i_rows

Solution 1:

You just have to assign the value against the col, not c

for row, r inenumerate(matrix):
    for col, c inenumerate(r):
        r[col] = 1# Note `col`, not `c`

Because the first value returned by enumerate will be the index and the second value will be the actual value itself.

Post a Comment for "Iterate Across Columns In A List Of Lists In Python"