Question
Without initially knowing the dimensions of a 2-D Array, how could we get the last element?
Answer
Assuming that the 2-D Array has the same number of elements for each row, such that it is an Array of size M x N
, we could do this using the len()
function. The len()
function works the same way for arrays as it does for lists in Python.
First, we can get the last row index like so.
len(array) - 1
And since each row has the same number of elements, we can get the index of the final column like so.
len(array[0]) - 1
Putting this together, selecting the last, or bottom-right most, element is done with
array[len(array) - 1, len(array[0]) - 1]