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 Set issubset()
Python Program to Print Colored Text to the Terminal
Python Program to Find the Size (Resolution) of a Image
Building Machine Learning Systems with Python - Willi Richert & Luis Pedro Coelho
Building Chatbots with Python Using Natural Language Processing and Machine Learning - Sumit Raj
Python super()
Python Operators
Learning scikit-learn Machine Learning in Python - Raul Garreta & Guillermo Moncecchi
Python max()
Python Program to Copy a File
Python for Loop
Python Program to Convert Two Lists Into a Dictionary
APIs in Node.js vs Python - A Comparison
Python Deep Learning Cookbook - Indra den Bakker
Python if...else Statement
Python Program to Sort Words in Alphabetic Order
Python Set pop()
Python Functions
Python bool()
Python ord()
Ways to Iterate Over a List in Java
Python Namespace and Scope
Python complex()
Python exec()
Python String partition()
Python frozenset()
Python bin()
Python Program to Remove Duplicate Element From a List
Python Exception Handling Using try, except and finally statement
Python Dictionary update()
Python String ljust()
Python Program to Create Pyramid Patterns