Bor
Bor

Reputation: 793

Iterate through jagged array values and indices in Python

Let's say arr is jugged array like this(list of lists if you want):

arr = [[10], [12, 3], [33, 22, 2], [2], ...]

Neither the size of the each row is known nor the count of lists.

How can one print the the indices and values getting the size of the row dynamically ? How to iterate so to print that arr[2][0] = 33 for example?

First try:

for value in arr:                                                           
      i = 0                                                                   
      j = 0                                                                   
      if isinstance(value, list):                                             
          for other in value:                                                 
              print("a[%s][%s] = %s" % (i, j, other))                         

              i += 1                                                          
              j += 1 

Every new row start with indices [0][0]. Value 12 should be [1][0]

a[0][0] = 10
a[0][0] = 12
a[1][1] = 3
... 

Second try. First row is off by 1. Value 10 should be with indices [0][0]

i = 0                                                                             
for value in arr1:                                                                   
    i += 1                                                                           
    for j in range(0, len(value)):                                              
        print("a[%s][%s] = %s" % (i, j, value[j]))  

Results:

a[1][0] = 10
a[2][0] = 12
a[2][1] = 3

Upvotes: 1

Views: 3590

Answers (2)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476813

The first code snippet fails because you reset i in every outer iteration:

for value in arr:                                                           
      i = 0 # reset the counter?
      j = 0                                                                   
      if isinstance(value, list):                                             
          for other in value:                                                 
              print("a[%s][%s] = %s" % (i, j, other))                         

              i += 1 # increment in the inner loop?
              j += 1

The second code snippet fails because you increment too early:

i = 0                                                                             
for value in arr1:                                                                   
    i += 1 # increment *before* the inner loop?
    for j in range(0, len(value)):                                              
        print("a[%s][%s] = %s" % (i, j, value[j]))

Nevertheless you can simply use enumerate(..) and make things easier:

for i,value in enumerate(arr1):
    for j,item in enumerate(value):
        print("a[%s][%s] = %s" % (i, j, item))

enumerate(..) takes as input an iterable and generates tuples containing the index (as first item of the tuple) and the element. So enumerate([1,'a',2,5.0]), it generates tuples (0,1), (1,'a'), (2,2) and (3,5.0).

Upvotes: 3

Jared Goguen
Jared Goguen

Reputation: 9010

Use enumerate.

jagged = [[1], [2, 3], [4, 5, 6]]

for i, sub_list in enumerate(jagged):
    for j, value in enumerate(sub_list):
        print 'a[{}][{}] = {}'.format(i, j, value)


# a[0][0] = 1
# a[1][0] = 2
# a[1][1] = 3
# a[2][0] = 4
# a[2][1] = 5
# a[2][2] = 6

Upvotes: 6

Related Questions