In this example, you will learn to get the last element of this list.
To understand this example, you should have the knowledge of the following Python programming topics:
Using negative indexing
my_list = ['a', 'b', 'c', 'd', 'e'] # print the last element print(my_list[-1])
Output
e
When you use negative indexing, the counting starts from 1 not 0 as shown in the figure below.
If you want the first 1st element, you can use my_list[-5]
.
If you want to learn more, please go to Python list Negative Indexing.
Related posts:
Python any()
Python Dictionary fromkeys()
Python Program to Find the Sum of Natural Numbers
Python String title()
Python Program to Add Two Matrices
How to Convert List to Map in Java
Python Functions
Python List extend()
Python Global Keyword
Python pass statement
Python del Statement
Python Program to Convert Decimal to Binary, Octal and Hexadecimal
Python while Loop
Converting Between a List and a Set in Java
Python Machine Learning - Sebastian Raschka
How to get current date and time in Python?
Python Program to Find Sum of Natural Numbers Using Recursion
Python Dictionary update()
Python Program to Make a Flattened List from Nested List
Python max()
Python Tuple
Python Set difference()
Python Program to Get the Full Path of the Current Working Directory
Python compile()
Python Dictionary copy()
Python Set discard()
Python enumerate()
Python locals()
Python hex()
Python String lower()
Python Program to Convert Two Lists Into a Dictionary
Python Program to Concatenate Two Lists