Reputation: 127
I am building a function that is given a a 2d list and a location (which is a tuple), and returns the value at that location. I'm probably confusing myself because I know how to return the index, but the problem is I don't understand how to get the function to apply the location.
Example Input/Output:
get_value_at_location([[1]], (0,0)) → 1
get_value_at_location([[1,2],[None,1]], (1,0)) → None
My code isn't really set because I don't understand how to apply the location. Here is what I do have:
def get_value_at_location(puzzle,loc):
x= loc[0]
y= loc[1]
for items in range(len(puzzle)):
for val in items:
#go to location (x,y) and extract val
return val
Upvotes: 0
Views: 83
Reputation: 1947
As the data being stored in puzzle (2d list), so you have to first get the row from which you need to search the value which in this case is loc[0], and then the column which is loc[1].
puzzle = [[1,2],[None,1]]
1st row: puzzle[0] = [1,2], puzzle[0][0] = 1, puzzle[0][1] = 2
2nd row: puzzle[1] = [None, 1], puzzle[1][0] = None, puzzle[1][1] = 1
try this:
def get_value_at_location(puzzle,loc):
return str(puzzle[loc[0]][loc[1]])
Upvotes: 1