Saturday, November 16, 2024
Google search engine
HomeLanguagesPython – Negative index of Element in List

Python – Negative index of Element in List

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




# Python3 code to demonstrate working of
# Negative index of Element
# Using index() + len()
 
# initializing list
test_list = [5, 7, 8, 2, 3, 5, 1]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing Element
K = 3
 
# getting length using len() and subtracting index from it
res = len(test_list) - test_list.index(K)
 
# printing result
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




# Python3 code to demonstrate working of
# Negative index of Element
# Using ~ operator + list slicing + index()
 
# initializing list
test_list = [5, 7, 8, 2, 3, 5, 1]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing Element
K = 3
 
# -1 operator to reverse list, index() used to get index
res = ~test_list[::-1].index(K)
 
# printing result
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
 
# initialize index variable
index = -1
 
# iterate over the list
for i, elem in enumerate(test_list):
    if elem == K:
        index = -(i+1# calculate negative index and break loop
        break
 
# print the result
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




# Python3 code to demonstrate working of
# Negative index of Element
# Using enumerate() and loop
 
# initializing list
test_list = [5, 7, 8, 2, 3, 5, 1]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing Element
K = 3
 
# iterating over the list using enumerate()
for i, val in enumerate(test_list):
    # checking if current value is equal to target value
    if val == K:
        # calculating negative index using length of list and current index
        res = -(len(test_list) - i)
        # printing result and breaking out of loop
        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




# Python3 code to demonstrate working of
# Negative index of Element
# Using for loop and range()
 
# initializing list
test_list = [5, 7, 8, 2, 3, 5, 1]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing Element
K = 3
 
# using for loop and range function to find the negative index
for i in range(len(test_list)-1, -1, -1):
    if test_list[i] == K:
        res = len(test_list) - i
        break
 
# printing result
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).

RELATED ARTICLES

Most Popular

Recent Comments