The Wayback Machine - https://web.archive.org/web/20240717024525/https://www.geeksforgeeks.org/python-negative-index-of-element-in-list/
Open In App

Python – Negative index of Element in List

Last Updated : 20 Mar, 2023
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

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).



Similar Reads

Python | Replace elements in second list with index of same element in first list
Given two lists of strings, where first list contains all elements of second list, the task is to replace every element in second list with index of elements in first list. Method #1: Using Iteration C/C++ Code # Python code to replace every element # in second list with index of first element. # List Initialization Input1 = ['cut', 'god', 'pass']
5 min read
Python - Find the index of Minimum element in list
Sometimes, while working with Python lists, we can have a problem in which we intend to find the position of minimum element of list. This task is easy and discussed many times. But sometimes, we can have multiple minimum elements and hence multiple minimum positions. Let’s discuss ways to achieve this task. Method #1: Using min() + enumerate() + l
5 min read
Python program to compute the power by Index element in List
Given a list, the task is to write a Python program to compute the power of each element by its index value. Input : test_list = [6, 9, 1, 8, 4, 7]Output : [1, 9, 1, 512, 256, 16807]Explanation : 8 * 8 * 8 = 512, as 8 is at 3rd index. Input : test_list = [6, 9, 1, 8]Output : [1, 9, 1, 512]Explanation : 9**1 = 9, as 9 is at 1st index. Method 1 : Usi
6 min read
Python - Key with Maximum element at Kth index in Dictionary Value List
Given a dictionary with values as lists, the task is to write a Python program to get the key with the maximum element at the Kth index by comparing the elements of each list. Input : test_dict = {'Gfg' : [4, 6, 8, 2], 'is' : [1, 4, 5, 9], 'best' :[2, 3, 4, 10], 'for' :[4, 5, 2, 1], 'geeks' :[2, 10, 1, 8]}, K = 3 Output : best Explanation : 2, 9, 1
8 min read
Python | Print list after removing element at given index
In this article, we will see how we can remove elements at a given index and print the list afterward. Example Input1: list = [10, 20, 30, 40, 50] index = 2 Output1: [10, 20, 40, 50] Input2: list = [10, 20, 40, 50] index = 0 Output2: [20, 40, 50] Print list after removing element at given indexBelow are the methods by which we can remove element at
3 min read
Remove an Element from a List by Index in Python
We are given a list and an index value. We have to remove an element from a list that is present at that index value in a list in Python. In this article, we will see how we can remove an element from a list by using the index value in Python. Example: Input: [1, 2, 3, 4, 5], index = 2 Output: [1, 2, 4, 5] Explanation: Element present in 2nd index
3 min read
Python program to count positive and negative numbers in a list
Given a list of numbers, write a Python program to count positive and negative numbers in a List. Example: Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3 Input: list2 = [-12, 14, 95, 3] Output: pos = 3, neg = 1 Example #1: Count positive and negative numbers from the given list using for loop Iterate each element in the list using for
6 min read
Python program to print negative numbers in a list
Given a list of numbers, write a Python program to print all negative numbers in the given list. Example: Input: list1 = [12, -7, 5, 64, -14] Output: -7, -14 Input: list2 = [12, 14, -95, 3] Output: -95 Example #1: Print all negative numbers from the given list using for loop Iterate each element in the list using for loop and check if the number is
5 min read
Python | Counting sign change in list containing Positive and Negative Integers
Given a list containing Positive and Negative integers, We have to find number of times the sign(Positive or Negative) changes in the list. Input: [-1, 2, 3, -4, 5, -6, 7, 8, -9, 10, -11, 12] Output:9 Explanation : Sign change from -1 to 2, ans = 1 Sign change from 3 to -4, ans = 2 Sign change from -4 to 5, ans = 3 Sign change from 5 to -6, ans = 4
5 min read
Python - Remove Negative Elements in List
Sometimes, while working with Python lists, we can have a problem in which we need to remove all the negative elements from list. This kind of problem can have application in many domains such as school programming and web development. Let's discuss certain ways in which this task can be performed. Input : test_list = [6, 4, 3] Output : [6, 4, 3] I
5 min read
Practice Tags :
three90RightbarBannerImg