Python Negative index of Element in List (original) (raw)
Last Updated : 29 Jan, 2025
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.
**Using 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.
Python `
li = [10, 20, 30, 40, 50] ele = 30
Find the positive index and convert it to negative
p = li.index(ele) n = -(len(li) - p) - 1 print(f"Negative index of {ele}: {n}")
`
Output
Negative index of 30: -3
**Explanation:
- Code reverses list using
li[::-1]
and then usesindex()
method to find index of the element from reversed list. - Negative index is calculated by subtracting index of element from length of the list adjusted for a negative index.
Using 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.
Python `
li = [10, 20, 30, 40, 50] ele = 30
Reverse the list and find index
n = -list(reversed(li)).index(ele) - 1 print(f"Negative index of {ele}: {n}")
`
Output
Negative index of 30: -3
**Explanation:
- List is reversed using
reversed()
and theindex()
function is used to find index of the element in the reversed list. - Negative index is obtained by negating the result of the index in the reversed list and subtracting one to adjust for the reversed order.
Using a loop
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.
Python `
li = [10, 20, 30, 40, 50] ele = 30
Loop through the list and calculate the negative index
n = -1 for i in range(len(li)-1, -1, -1): if li[i] == ele: n = -(len(li) - i) break
print(f"Negative index of {ele}: {n}")
`
Output
Negative index of 30: -3
**Explanation:
- Loop iterates over list in reverse order starting from last element and moving towards the first. It checks each element to see if it matches the target element (
ele
). - When the element is found the negative index is calculated by subtracting the current index from the total length of the list ensuring the result is in negative form.
Similar Reads
- Remove Negative Elements in List-Python The task of removing negative elements from a list in Python involves filtering out all values that are less than zero, leaving only non-negative numbers. Given a list of integers, the goal is to iterate through the elements, check for positivity and construct a new list containing only positive num 3 min read
- Python - Indices of atmost K elements in list Many times we might have problem in which we need to find indices rather than the actual numbers and more often, the result is conditioned. First approach coming to mind can be a simple index function and get indices less than or equal than particular number, but this approach fails in case of dupli 7 min read
- Find index of element in array in python We often need to find the position or index of an element in an array (or list). We can use an index() method or a simple for loop to accomplish this task. index() method is the simplest way to find the index of an element in an array. It returns the index of the first occurrence of the element we a 2 min read
- Find Index of Element in Array - Python In Python, arrays are used to store multiple values in a single variable, similar to lists but they offer a more compact and efficient way to store data when we need to handle homogeneous data types . While lists are flexible, arrays are ideal when we want better memory efficiency or need to perform 2 min read
- Python - Find starting index of all Nested Lists In this article given a matrix, the task is to write a Python program to compute the starting index of all the nested lists. Example: Input : test_list = [[5], [9, 3, 1, 4], [3, 2], [4, 7, 8, 3, 1, 2], [3, 4, 5]] Output : [0, 1, 5, 7, 13] Explanation : 1 + 4 = lengths of 2 initial lists = 5, 3, of 3 8 min read
- Get the Last Element of List in Python In this article, we will learn about different ways of getting the last element of a list in Python. For example, consider a list: Input: list = [1, 3, 34, 12, 6]Output: 6 Explanation: Last element of the list l in the above example is 6. Let's explore various methods of doing it in Python: 1. Using 2 min read
- Python - Elements Maximum till current index in List Given list with elements, extract element if it's the maximum element till current index. Input : test_list = [4, 6, 7, 8] Output : [4, 6, 7, 8] Explanation : All elements are maximum till their index. Input : test_list = [6, 7, 3, 6, 8, 7] Output : [7, 8] Explanation : 7 and 8 are maximum till thei 7 min read
- Python - Get Indices of Even Elements from list Sometimes, while working with Python lists, we can have a problem in which we wish to find Even elements. This task can occur in many domains such as web development and while working with Databases. We might sometimes, require to just find the indices of them. Let us discuss certain ways to find in 8 min read
- Handling " No Element Found in Index() " - Python The task of handling the case where no element is found in the index() in Python involves safely managing situations where the desired element does not exist in a list. For example, with the list a = [6, 4, 8, 9, 10] and the element 11, we want to ensure the program doesn't raise an error if the ele 3 min read
- Python | Check for Nth index existence in list Sometimes, while working with lists, we can have a problem in which we require to insert a particular element at an index. But, before that it is essential to know that particular index is part of list or not. Let's discuss certain shorthands that can perform this task error free. Method #1 : Using 3 min read