Is it possible to find the exact positions of a certain item in a nested loop?

Is it possible to find all the positions of HSE in the nested loop below?

map1 = [ ['   ', '   ', '   ', '   '],\
         ['   ', '   ', 'SHP', '   '],\
         ['   ', 'HSE', 'HSE', 'FAC'],\
         ['   ', 'BCH', '   ', 'HSE'] ]

I am only aware of using index, but it returns the first value only. Below is my codes:

HSE_list = []

for x in map1:
    if 'HSE' in x:
         y = (map1.index(x), x.index('HSE'))

It returns the first item in every list which comes in contact with, so no matter how many HSE I have in the nested loop, it will still only show the first HSE that it sees in map[0],map[1],map[2],map[3]. My print will return as: [(2, 1), (3, 3)] when I run the program.

is there any way that I can make it show all the positions of the house in my nested list, [(2, 1), (2, 2), (3, 3)]?

Read more here:

Content Attribution

This content was originally published by user16472631 at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: