 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python – Negative index of Element in List
When it is required to get the negative index of an element in a list, the ‘len’ method and the ‘index’ method are used.
Below is a demonstration of the same −
Example
my_list = [52, 47, 18, 22, 23, 57, 13] print("The list is :") print(my_list) my_key = 22 print("The value of key is ") print(my_key) my_result = len(my_list) - my_list.index(my_key) print("The result is :") print(my_result)  Output
The list is : [52, 47, 18, 22, 23, 57, 13] The value of key is 22 The result is : 4
Explanation
- A list of integers is defined and is displayed on the console. 
- A key is defined and is displayed on the console. 
- The difference between the length of the list and element present at specific key is determined. 
- This is assigned to a variable. 
- This is displayed as output on the console. 
Advertisements
 