HELP on outputting 2D arrays in python?

Hi, so I have this piece of code that I want to get the row, column and value in a 2D array and print it but I have been stuck on it for some time so I would appreciate it if anyone can point out on whats wrong with it, thank you.

myNumbers = [[20, 25, 30, 35, 40], [2, 3, 4, 5, 6]]

for column in myNumbers:
  for elem in column:
    for row in range(len(myNumbers)):
      for column in range(len(myNumbers[0])):
        print("The value at row %s column %s is %s" % (row, column, elem))
Output

The value at row 0 column 0 is 20
The value at row 0 column 1 is 20
The value at row 0 column 2 is 20
The value at row 0 column 3 is 20
The value at row 0 column 4 is 20
The value at row 1 column 0 is 20
The value at row 1 column 1 is 20
The value at row 1 column 2 is 20
The value at row 1 column 3 is 20
The value at row 1 column 4 is 20
The value at row 0 column 0 is 25
The value at row 0 column 1 is 25
The value at row 0 column 2 is 25
The value at row 0 column 3 is 25
The value at row 0 column 4 is 25
The value at row 1 column 0 is 25
The value at row 1 column 1 is 25
The value at row 1 column 2 is 25
The value at row 1 column 3 is 25
The value at row 1 column 4 is 25
The value at row 0 column 0 is 30
The value at row 0 column 1 is 30
The value at row 0 column 2 is 30
The value at row 0 column 3 is 30
The value at row 0 column 4 is 30
The value at row 1 column 0 is 30
The value at row 1 column 1 is 30
The value at row 1 column 2 is 30
The value at row 1 column 3 is 30
The value at row 1 column 4 is 30
The value at row 0 column 0 is 35
The value at row 0 column 1 is 35
The value at row 0 column 2 is 35
The value at row 0 column 3 is 35
The value at row 0 column 4 is 35
The value at row 1 column 0 is 35
The value at row 1 column 1 is 35
The value at row 1 column 2 is 35
The value at row 1 column 3 is 35
The value at row 1 column 4 is 35
The value at row 0 column 0 is 40
The value at row 0 column 1 is 40
The value at row 0 column 2 is 40
The value at row 0 column 3 is 40
The value at row 0 column 4 is 40
The value at row 1 column 0 is 40
The value at row 1 column 1 is 40
The value at row 1 column 2 is 40
The value at row 1 column 3 is 40
The value at row 1 column 4 is 40
The value at row 0 column 0 is 2
The value at row 0 column 1 is 2
The value at row 0 column 2 is 2
The value at row 0 column 3 is 2
The value at row 0 column 4 is 2
The value at row 1 column 0 is 2
The value at row 1 column 1 is 2
The value at row 1 column 2 is 2
The value at row 1 column 3 is 2
The value at row 1 column 4 is 2
The value at row 0 column 0 is 3
The value at row 0 column 1 is 3
The value at row 0 column 2 is 3
The value at row 0 column 3 is 3
The value at row 0 column 4 is 3
The value at row 1 column 0 is 3
The value at row 1 column 1 is 3
The value at row 1 column 2 is 3
The value at row 1 column 3 is 3
The value at row 1 column 4 is 3
The value at row 0 column 0 is 4
The value at row 0 column 1 is 4
The value at row 0 column 2 is 4
The value at row 0 column 3 is 4
The value at row 0 column 4 is 4
The value at row 1 column 0 is 4
The value at row 1 column 1 is 4
The value at row 1 column 2 is 4
The value at row 1 column 3 is 4
The value at row 1 column 4 is 4
The value at row 0 column 0 is 5
The value at row 0 column 1 is 5
The value at row 0 column 2 is 5
The value at row 0 column 3 is 5
The value at row 0 column 4 is 5
The value at row 1 column 0 is 5
The value at row 1 column 1 is 5
The value at row 1 column 2 is 5
The value at row 1 column 3 is 5
The value at row 1 column 4 is 5
The value at row 0 column 0 is 6
The value at row 0 column 1 is 6
The value at row 0 column 2 is 6
The value at row 0 column 3 is 6
The value at row 0 column 4 is 6
The value at row 1 column 0 is 6
The value at row 1 column 1 is 6
The value at row 1 column 2 is 6
The value at row 1 column 3 is 6
The value at row 1 column 4 is 6

What I want

The value at row 0 column 0 is 20
The value at row 0 column 1 is 25
The value at row 0 column 2 is 30
The value at row 0 column 3 is 35
The value at row 0 column 4 is 40
The value at row 1 column 0 is 2
The value at row 1 column 1 is 3
The value at row 1 column 2 is 4
The value at row 1 column 3 is 5
The value at row 1 column 4 is 6

I have also tried this but it didn’t seem to work so I probably didn’t use for loops properly

myNumbers = [[20, 25, 30, 35, 40], [2, 3, 4, 5, 6]]

for row in myNumbers:
  for elem in row:
    elem = elem
    
for row in range(len(myNumbers)):
  for column in range(len(myNumbers[0])):
    print("The value at row %d column %d is %s" % (row, column, elem))
Output

The value at row 0 column 0 is 6
The value at row 0 column 1 is 6
The value at row 0 column 2 is 6
The value at row 0 column 3 is 6
The value at row 0 column 4 is 6
The value at row 1 column 0 is 6
The value at row 1 column 1 is 6
The value at row 1 column 2 is 6
The value at row 1 column 3 is 6
The value at row 1 column 4 is 6

What I want

The value at row 0 column 0 is 20
The value at row 0 column 1 is 25
The value at row 0 column 2 is 30
The value at row 0 column 3 is 35
The value at row 0 column 4 is 40
The value at row 1 column 0 is 2
The value at row 1 column 1 is 3
The value at row 1 column 2 is 4
The value at row 1 column 3 is 5
The value at row 1 column 4 is 6

You don’t need more than 2 loops here. In first one get row index while iterating length of the myNumbers array. The second one use to get index of the column while iterating length of the row - use the row index from the previous loop to get appropriate list form the myNumbers array. Finally in that second loop print also the output, you have all what you need both row and column indices, just use them correctly with the myNumbers array.

I don’t know if I followed it properly but:

for row in myNumbers:
  for column in row:
    print("The value at row %s column %s is %s" % (row, column, myNumbers))
Output

The value at row [20, 25, 30, 35, 40] column 20 is [[20, 25, 30, 35, 40], [2, 3, 4, 5, 6]]
The value at row [20, 25, 30, 35, 40] column 25 is [[20, 25, 30, 35, 40], [2, 3, 4, 5, 6]]
The value at row [20, 25, 30, 35, 40] column 30 is [[20, 25, 30, 35, 40], [2, 3, 4, 5, 6]]
The value at row [20, 25, 30, 35, 40] column 35 is [[20, 25, 30, 35, 40], [2, 3, 4, 5, 6]]
The value at row [20, 25, 30, 35, 40] column 40 is [[20, 25, 30, 35, 40], [2, 3, 4, 5, 6]]
The value at row [2, 3, 4, 5, 6] column 2 is [[20, 25, 30, 35, 40], [2, 3, 4, 5, 6]]
The value at row [2, 3, 4, 5, 6] column 3 is [[20, 25, 30, 35, 40], [2, 3, 4, 5, 6]]
The value at row [2, 3, 4, 5, 6] column 4 is [[20, 25, 30, 35, 40], [2, 3, 4, 5, 6]]
The value at row [2, 3, 4, 5, 6] column 5 is [[20, 25, 30, 35, 40], [2, 3, 4, 5, 6]]
The value at row [2, 3, 4, 5, 6] column 6 is [[20, 25, 30, 35, 40], [2, 3, 4, 5, 6]]

What I'm trying to get

The value at row 0 column 0 is 20
The value at row 0 column 1 is 25
The value at row 0 column 2 is 30
The value at row 0 column 3 is 35
The value at row 0 column 4 is 40
The value at row 1 column 0 is 2
The value at row 1 column 1 is 3
The value at row 1 column 2 is 4
The value at row 1 column 3 is 5
The value at row 1 column 4 is 6

Not quite. Use range and len functions when iterating. That way for row and column you will have numbers. Remember than when list is nested you can get elements by using multiple indices at the same time, i.e. some_array[0][1].

1 Like