Once we have a grid established, we can use the structure to navigate the data and establish relationships based on proximity. One of the key concepts of grids is that we can address neighboring cells.

Let's look at how to access the neighbors of a cell:

Notice that we can identify a neighbor relation using the index of a cell.

Depending on the type of grid structure we are using, the notation of the neighbors would change, but we should consider them to be a relation to the index. Often the relationship is of a "-1" or "+1" in nature.

Let's see this in Python - Let's start by creating a grid:

#define the resolution of a grid
reso_x = 10
reso_y = 10

#Let's start with an empty list
my_grid_list = []


#Let's create some counting variables
int_current_x = 0
int_current_y = 0

#Define the size of a Cell 
cell_size_x = 5
cell_size_y = 5

#We'll do a single loop to generate a flat list
for i in range(0, reso_x * reso_y):
        
    #Define the position of a cell by the i and j iteration provided in the loop, multiplied by the size of the cell
    pos_x = (int_current_x * cell_size_x) 
    pos_y = (int_current_y * cell_size_y)

    #Let's store our position data
    my_grid_list.append( (pos_x, pos_y) )
        

    #Let's update our count:
    #Each iteration of the loop we increase x by 1
    int_current_x += 1

    #If x becomes larger than the intended resolution in x, it returns back to 0, and y increases by 1
    if(int_current_x >= reso_x):
        int_current_x = 0
        int_current_y += 1

Based on the grid we described above, we can consider the following neighbors:

#lets define an arbitrary index to evaluate
int_index = 15

#Right neighbor
my_grid_list [ int_index + 1 ]

#Left Neighbor
my_grid_list [ int_index - 1 ]

#Neighbor above
my_grid_list [ int_index - reso_x ]

#Neighbor below
my_grid_list [ int_index + reso_x ]

A few things to notice:

1 - These neighbors will not always work, as we need to take into consideration that a grid has EDGES or a BORDER condition. We will need to exclude the edges from this calculation.

2 - Notice that the neighbor above and below needs to be calculated using the length of the columns in the list.

See if you can define an expression for neighbors of a nested list.