I want to go through just a section of a 2D list, rather than the whole thing.
Here's essentially what it is I want to do:
Let's say the user inputs the coordinates [1,1]
(so, row 1 column 1)
If I have the 2D list:
[[1,3,7],
[4,2,9],
[13,5,6]]
Then I want to iterate through all the elements adjacent to the element at [1,1]
Furthermore, if the element is at a corner or the edge of the 2D list, (so basically if the user enters [0,0],
for example) then I want to just want to get back the elements at [0,0]
, [0,1]
, [1,0]
, and [1,1]
So essentially I just want elements adjacent to a specific to a certain point in the 2D array.
Here's what I've done so far:
I've made it so that it assigns 4 variables at the start of the code: starting_row
, ending_row
, starting_column
, and ending_column
. These variables are assigned values based off of which coordinates the user wants to input (if they the row is 0 or len(list) then the for loop runs accordingly. The same goes for the columns).
Then, I use a nested for loop to go through every element
for row in range(row_start, row_end+1):
for column in range(column_start, column_end+1):
print(lst[row,column])
Only thing is, it doesn't seem to work correctly and often outputs the whole entire 2D list when enter a list size of more than 3x3 elements (all the lists will be square lists)