The method index raises a
In addition, you have a 2d list, where you can't just use the method
Before you use the method
Loop over the rows and you get the columns.
For each column you look if the value you seek is in the row.
If this is the case, just return the row_idx and col_idx.
ValueError
, if the value does not exist in the list.In addition, you have a 2d list, where you can't just use the method
index
.Before you use the method
index
, you could look up with the in
operator, if the value is in the list.my_list = [1, 2, 3] # code if 2 in my_list: print("Index:", my_list.index(2))But you have a 2d-list. You can nest for-loops, but it's not required in this case.
Loop over the rows and you get the columns.
For each column you look if the value you seek is in the row.
If this is the case, just return the row_idx and col_idx.
test = ["54", "cat", "99", "1238"], ["758", "tre", "1233124"], ["hva skjer", "15684", "0"] def index2d(matrix, value): for row_idx, row in enumerate(matrix): if value in row: return row_idx, row.index(value) index2d(test, "99")
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!