title: How to iterate or deal with grid or coordinate in python
body: <p>I have created a forest with random number in a grid of N x N. For creating the forest, I have taken each grid and a random number to plant a tree. And store the planted coordinate in a list <code>li[]</code> for x-axis and <code>lj[]</code> for y-axis. And also put the coordinate in a .dat file</p>
  
  <p>In python, here is my code:</p>
  
  <pre><code>from math import *
  from random import random
  
  n = input('Choose grid, N = ')
  p = 0.5
  
  li = []
  lj = []
  
  fid = open('plant.dat','w')
  for i in range(1,n+1):
      for j in range(1,n+1):
          x = random()
          if x&lt;p:
              li.append(i)
              lj.append(j)
  
              print&gt;&gt;fid,i,j
              print (i,j),'is',1
          else:
              print (i,j),'is',0
          print (i,j)
          print (p,x,n),'and',n*n*p,li,lj
  </code></pre>
  
  <p>Now I want to fire it. I start with the bottom points and set them on fire. And from there the fire spread in the all four direction to the adjacent points in the forest. Not in diagonal direction.</p>
  
  <p>for that I check where y-axis is 1, it is the bottom point, in the list <code>lj[]</code> and put that point as fired.</p>
  
  <p>Now I have to spread the fire to the adjacent tree (not in the diagonal) direction:</p>
  
  <p>Here I'm stucked and don't know how to do this. Basically I want to follow the fired point in the bottom line and increase the value of x-axis and y-axis and check its adjacent point.
  But it shows only the bottom line.</p>
  
  <p>Here is my code :</p>
  
  <pre><code>print 'Now we are firing'
  lif=[]
  ljf=[]
  fid1 = open('fire.dat','w')
  for b in range(len(lj)):
      if lj[b] == 1:
          print 'We fire here',(li[b],lj[b])
          print&gt;&gt;fid1,li[b],lj[b]
          lif.append(li[b])
          ljf.append(lj[b])
          #for e in range(2,n+1):
          for r in li:
              if li[r] == li:
                  print 'fire spread at',(li[r],lj[r])
                  print &gt;&gt; fid1,li[r],lj[r]
                  lif.append(li[r])
                  ljf.append(lj[r])
  </code></pre>
  
  <p>here is my plot for 'plant.dat' file , by gnuplot:
  <img src="https://i.stack.imgur.com/8NSK4.png" alt="enter image description here"></p>
  
  <p>In the above picture , clearly fire can go up to a certain extend and stop, leaving many living trees. </p>
  
  <p>I want the coordinate of that living trees only and plot it.</p>
  
  <p>For the particular input plot shown above, the final plot with the remaining living trees should look like as follows:</p>
  
  <p><img src="https://i.stack.imgur.com/fglJo.png" alt="enter image description here"></p>
  
  <p>The partially erased points are fired and shouldn't be there.</p>