how to loop through nested array with None type

I’m trying to go through each row of my field and then each column within the row, but my field is made of None for the empty spaces that I need. My code gives a type error NoneType object is unsubscriptable, so how can I go about skipping the Nones in my field?

field = [ [['PEASH', 5], None, None, None, None, None, ['ZOMB', 15]],
          [['PEASH', 5], None, None, None, None, None, ['ZOMB', 15]],
          [None, None, None, None, None, None, None],
          [None, None, None, None, None, None, None],
          [None, None, None, None, None, None, None] ]

num_rows=len(field)
num_columns=len(field[0])

lane = 0
row_counter = -1
column_counter = -1
for row in range(num_rows):
    row_counter += 1
    print('rowcounter is at',row_counter)
    print(row)
    for column in range(num_columns): #for each column, 
        column_counter += 1

        element = field[row][column] 
        print(element) #now element will be ['peash',15]
        if element[0] == 'PEASH':
            print('yes there is a peashooter in',row_counter,column_counter)
            

>Solution :

simply change

if element[0] == 'PEASH':
    print('yes there is a peashooter in',row_counter,column_counter)

to

if element is not None:
    if element[0] == 'PEASH':
        print('yes there is a peashooter in',row_counter,column_counter)

Leave a Reply