title: Finding all indexes of a list of lists in python
body: <p>I searched the forums and tried a couple of the answers but it does not seem to work for me.</p>
  
  <p>So my problem is I have a list of lists which has 0 and 'M' in at different places every time you run the program.The position of the 'M' is randomized.</p>
  
  <p>So I wanna get the indexes of all the positions of where the 'M' is in the list.</p>
  
  <p>I have tried this:</p>
  
  <pre><code>hiddenfield =  [[0, 0, 0, 0, 0, 'M', 0, 0, 0, 'M'],
                  [0, 0, 0, 0, 0, 'M', 0, 'M', 'M', 'M'],
                  [0, 0, 'M', 0, 'M', 0, 0, 'M', 0, 'M'],
                  [0, 0, 0, 0, 'M', 0, 0, 0, 0, 0],
                  [0, 0, 'M', 0, 'M', 0, 0, 0, 0, 0],
                  [0, 0, 0, 'M', 0, 'M', 'M', 0, 0, 0],
                  [0, 0, 0, 'M', 0, 0, 0, 'M', 0, 0],
                  [0, 0, 0, 'M', 0, 0, 0, 0, 0, 0],
                  [0, 'M', 'M', 0, 'M', 0, 0, 0, 0, 0],
                  ['M', 0, 0, 'M', 0, 0, 0, 0, 'M', 0]] 
  
  position_of_m = []
  
  for i in hiddenfield:
          position_of_m.append(i.index('M'))
  print(position_of_m)
  </code></pre>
  
  <p>and all i get is the first index of all of the lists like this:</p>
  
  <pre><code>[5, 5, 2, 4, 2, 3, 3, 3, 1, 0]
  </code></pre>
  
  <p>how do i solve this?</p>
  
  <p>EDIT:</p>
  
  <p>Ok guys so i got some help from you earlier. But right now i am banging my head against the table.</p>
  
  <p>The code works 'kinda'. But i don't want the last numbers of the list to add to the first number of another list. Hard for me to explain so i´ll show you further down!</p>
  
  <p>heres the code:</p>
  
  <pre><code>hiddenfield =  [[0, 0, 0, 0, 0, 'M', 0, 0, 0, 'M'],
              [0, 0, 0, 'M', 0, 0, 0, 'M', 'M', 'M'],
              [0, 0, 'M', 0, 'M', 0, 0, 'M', 0, 'M'],
              [0, 0, 0, 0, 'M', 0, 0, 0, 0, 0],
              [0, 0, 'M', 0, 'M', 0, 0, 0, 0, 0],
              [0, 0, 0, 'M', 0, 'M', 'M', 0, 0, 0],
              ['M', 0, 0, 'M', 0, 0, 0, 'M', 0, 0],
              [0, 0, 0, 'M', 0, 0, 0, 0, 0, 0],
              [0, 'M', 'M', 0, 'M', 0, 0, 0, 0, 'M'],
              ['M', 0, 0, 'M', 0, 0, 0, 0, 'M', 0]] 
  
  
  
  def update_numbers(x, y, hiddenfield):
  try:
      if hiddenfield[x][y] != 'M':
          hiddenfield[x][y] += 1
  except IndexError:
      pass
  
  def numbersinhiddefield(indexes):
  indexes = [[i,j] for i,row in enumerate(indexes) for j,elem in enumerate(row) if elem == 'M']
  loop = 0
  while loop &lt; len(indexes):
      update_numbers(indexes[loop][0]+1,indexes[loop][1], hiddenfield)
      update_numbers(indexes[loop][0]-1,indexes[loop][1], hiddenfield)
      update_numbers(indexes[loop][0],indexes[loop][1]+1, hiddenfield)
      update_numbers(indexes[loop][0],indexes[loop][1]-1, hiddenfield)
      update_numbers(indexes[loop][0]+1,indexes[loop][1]+1, hiddenfield)
      update_numbers(indexes[loop][0]-1,indexes[loop][1]-1, hiddenfield)
      update_numbers(indexes[loop][0]+1,indexes[loop][1]-1, hiddenfield)
      update_numbers(indexes[loop][0]-1,indexes[loop][1]+1, hiddenfield)
      loop += 1
  
  
  def showMineFieldHidden(hiddenfield):
  border = list(range(0,len(hiddenfield)))
  row = [' ']+border
  i = 0
  for rows in [border]+hiddenfield:
      print(row[i], end=' ')
      i += 1
      for lines in rows:
          print(lines, end=' ')
      print()
  
  numbersinhiddefield(hiddenfield)
  
  showMineFieldHidden(hiddenfield)
  </code></pre>
  
  <p>But the thing i get out isn't just right:</p>
  
  <pre><code>  0 1 2 3 4 5 6 7 8 9 
  0 0 0 1 1 2 M 2 2 4 M 
  1 0 1 2 M 3 2 3 M M M 
  2 0 1 M 4 M 2 2 M 5 M 
  3 0 2 2 5 M 3 1 1 2 1 
  4 0 1 M 4 M 4 2 1 0 0 
  5 1 2 3 M 4 M M 2 1 1 
  6 M 1 3 M 4 2 3 M 1 1 
  7 2 3 4 M 3 1 1 1 2 2 
  8 2 M M 4 M 1 0 1 2 M 
  9 M 3 3 M 3 2 1 1 M 4 &lt;-- this is supposed to be a 2.
              ^
              this is supposed to be a 1 and the one next to it a zero
  </code></pre>
  
  <p>I think it is adding from the borders to the lower list? </p>
  
  <p>Would appreciate som help!</p>