Python – Negative index of Element in List
Last Updated :
20 Mar, 2023
Given a list of elements, find its negative index in the List.
Input : test_list = [5, 7, 8, 2, 3, 5, 1], K = 2
Output : -4
Explanation : 2 is 4th element from rear.
Input : test_list = [5, 7, 8, 2, 3, 5, 1], K = 5
Output : -2
Explanation : 5 is 2nd element from rear.
Method #1 : Using index() + len()
In this, we get the index of the element using index(), and then subtract it from the list length to get the required result.
Python3
test_list = [ 5 , 7 , 8 , 2 , 3 , 5 , 1 ]
print ( "The original list is : " + str (test_list))
K = 3
res = len (test_list) - test_list.index(K)
print ( "The required Negative index : -" + str (res))
|
Output:
The original list is : [5, 7, 8, 2, 3, 5, 1]
The required Negative index : -3
Time Complexity: O(n)
Auxiliary Space: O(1)
Method #2 : Using ~ operator + list slicing + index()
In this, we reverse the list using slicing, and use ~ operator to get negation, index() is used to get the desired negative index.
Python3
test_list = [ 5 , 7 , 8 , 2 , 3 , 5 , 1 ]
print ( "The original list is : " + str (test_list))
K = 3
res = ~test_list[:: - 1 ].index(K)
print ( "The required Negative index : " + str (res))
|
Output:
The original list is : [5, 7, 8, 2, 3, 5, 1]
The required Negative index : -3
Time complexity: O(n), where n is the length of the input list test_list.
Auxiliary space: O(n), where n is the length of the input list test_list.
Method 3-
In this approach to find the negative index of an element in a list is to iterate over the list and keep track of the index using a variable.
In this approach, we use the built-in enumerate() function to iterate over the list and keep track of the index of each element. When we find the target element, we calculate the negative index using the formula -(i+1) and break out of the loop. If the element is not found in the list, the index variable remains at its initial value of -1. Finally, we print the negative index.
Python3
test_list = [ 5 , 7 , 8 , 2 , 3 , 5 , 1 ]
K = 3
index = - 1
for i, elem in enumerate (test_list):
if elem = = K:
index = - (i + 1 )
break
print ( "The required Negative index : " + str (index))
|
Output
The required Negative index : -5
Time complexity: O(n), where n is the length of the list test_list.
Auxiliary space: O(1), because we only use a constant amount of extra space to store the index variable.
Method #4: Using enumerate() and a loop
In this approach, we can use the built-in enumerate() function to iterate over the list and keep track of the index and value of each element. We can then check if the value of the current element is equal to the target value, and if so, calculate and return the negative index using the length of the list and the index variable.
Python3
test_list = [ 5 , 7 , 8 , 2 , 3 , 5 , 1 ]
print ( "The original list is : " + str (test_list))
K = 3
for i, val in enumerate (test_list):
if val = = K:
res = - ( len (test_list) - i)
print ( "The required Negative index : " + str (res))
break
|
Output
The original list is : [5, 7, 8, 2, 3, 5, 1]
The required Negative index : -3
Time complexity: O(n), where n is the length of the list.
Auxiliary Space: O(1), which is constant space.
Method 5: Using a for loop and range() function
This method uses a for loop and range function to traverse the list from the end to start, checking each element to find the index of the given element K. If the element is found, it calculates the negative index and breaks the loop.
Python3
test_list = [ 5 , 7 , 8 , 2 , 3 , 5 , 1 ]
print ( "The original list is : " + str (test_list))
K = 3
for i in range ( len (test_list) - 1 , - 1 , - 1 ):
if test_list[i] = = K:
res = len (test_list) - i
break
print ( "The required Negative index : -" + str (res))
|
Output
The original list is : [5, 7, 8, 2, 3, 5, 1]
The required Negative index : -3
Time complexity: O(n).
Auxiliary space: O(1).
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...