Sobes.tech
Junior — Middle

Is it possible to use negative indices to access list elements?

sobes.tech AI

Answer from AI

In Python and some other programming languages, negative indices can be used to access list elements starting from the end. For example, the index -1 refers to the last element, -2 to the second last, and so on.

Example in Python:

lst = [10, 20, 30, 40]
print(lst[-1])  # Outputs 40
print(lst[-2])  # Outputs 30

However, in some languages, such as Java or C#, negative indices are not supported and using a negative index will result in an error. Therefore, the use of negative indices depends on the specific language and its capabilities.

Is it possible to use negative indices to access list… - sobes.tech