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 complex()
Python Operators
Python Program to Count the Number of Digits Present In a Number
Python String format()
Python Set issuperset()
Python Program to Merge Mails
Python String casefold()
Python compile()
Python filter()
Remove All Occurrences of a Specific Value from a List
Python Set update()
Python List pop()
Python Program to Find Armstrong Number in an Interval
Python time Module
Python Program to Print the Fibonacci sequence
Python Program to Find LCM
Python map()
Python String isalnum()
Python Get Current time
Python Type Conversion and Type Casting
Python Program to Print Hello world!
Python ord()
Python String splitlines()
Python Program to Check Prime Number
APIs in Node.js vs Python - A Comparison
Python tuple()
Python Program to Print Colored Text to the Terminal
Python Anonymous / Lambda Function
Python Set intersection_update()
How to Convert List to Map in Java
Java – Get Random Item/Element From a List
Python String index()