Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading