When it is required to get the negative index of an element in a list, the ‘len’ method and the ‘index’ method are used.
Below is a demonstration of the same −
Example
my_list = [52, 47, 18, 22, 23, 57, 13] print("The list is :") print(my_list) my_key = 22 print("The value of key is ") print(my_key) my_result = len(my_list) - my_list.index(my_key) print("The result is :") print(my_result)
Output
The list is : [52, 47, 18, 22, 23, 57, 13] The value of key is 22 The result is : 4
Explanation
A list of integers is defined and is displayed on the console.
A key is defined and is displayed on the console.
The difference between the length of the list and element present at specific key is determined.
This is assigned to a variable.
This is displayed as output on the console.