Python – Negative index of Element in List

• Difficulty Level : Medium
• Last Updated : 20 Oct, 2020

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 listtest_list = [5, 7, 8, 2, 3, 5, 1] # printing original listprint("The original list is : " + str(test_list)) # initializing ElementK = 3 # getting length using len() and subtracting index from itres = len(test_list) - test_list.index(K) # printing resultprint("The required Negative index : -" + str(res))

Output:

The original list is : [5, 7, 8, 2, 3, 5, 1]
The required Negative index : -3

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 listtest_list = [5, 7, 8, 2, 3, 5, 1] # printing original listprint("The original list is : " + str(test_list)) # initializing ElementK = 3 # -1 operator to reverse list, index() used to get indexres = ~test_list[::-1].index(K) # printing resultprint("The required Negative index : " + str(res))

Output:

The original list is : [5, 7, 8, 2, 3, 5, 1]
The required Negative index : -3

My Personal Notes arrow_drop_up