nth()
Gets the element at index n of array.
Implementation
Args: array: List passed in by the user. index: Index of element to return. Returns: Returns the element at :attr:`index`.
Example
nth([1, 2, 3, 4], 0)
Expected output: 1
Alternative usage:
nth([3, 4, 5, 6], 2)
Expected output: 5
Source Code
def nth(array: List[T], index: int) -> Optional[T]:
length = len(array)
if index < 0:
idx = length + index
if 0 <= idx < length:
return array[idx]
else:
if 0 <= index < length:
return array[index]
return None