Skip to main content

map_negative_index

Maps a negative index to its positive equivalent based on the provided length, or returns the index unchanged if it is already non-negative.

def map_negative_index(
index: integer,
length: integer
) - > integer

Converts a potentially negative sequence index into its equivalent positive absolute position. This is used to support Python-style negative indexing where -1 represents the last element in a collection of a specific length.

Parameters

NameTypeDescription
indexintegerThe sequence index to be normalized, which may be negative.
lengthintegerThe total number of elements in the sequence used to calculate the positive offset.

Returns

TypeDescription
integerThe normalized non-negative index if the input was negative, or the original index if it was already positive.