![]() |
VOOZH | about |
We are given a list we need to find the negative index of that element. For example, we are having a list li = [10, 20, 30, 40, 50] and the given element is 30 we need to fin the negative index of it so that given output should be -3.
index()index() method in Python searches for the first occurrence of a specified element in a list and returns its index. In this example s.index(ele) directly finds and returns the index of the element ele in the list s.
Negative index of 30: -3
Explanation:
li[::-1] and then uses index() method to find index of the element from reversed list.reversed() and index()Using reversed() to reverse list and index() to find the index of element within reversed list provides an efficient way to determine the negative index.
Negative index of 30: -3
Explanation:
reversed() and the index() function is used to find index of the element in the reversed list.Manually iterating through list in reverse order we can find the index of the given element loop checks each element starting from the last one and when the element is found it calculates the negative index based on position from the end.
Negative index of 30: -3
Explanation:
ele).