In Python, how do you get the last element of a list?
We have many ways of getting the last element of the list:-
The simplest way is to use abc_list[-1]. Let me give you an example that how it works.
abc_list=[1, 2, 3]abc_list[-1]
abc_list=[1, 2, 3]
The same way you can get all the elements till first using this method.
Where n = index of an element from last that you want.
Below is the algorithm to get the first element of the list.
But if your list is empty when you perform this method then you will get indexNotFound Error. So make sure your list is not empty when you are trying to get elements of your list by this method.
The second method to get the last element is to use operator.itemgetter:
import operatorabc_list=[1, 2, 3]last = operator.itemgetter(-1)last(abc_list)
last = operator.itemgetter(-1)
You can also do by list slicing a slice of a list returns a new list so we can slice from -1 to the end if we are going to want the element in a new list:-
abc_list=[1, 2,3]abc_slice = abc_list[-1:]abc_slice
abc_slice = abc_list[-1:]
It has an upper hand that it does not fail if your list is empty. Instead of giving index not found Error like the above method it will return you an empty list.
To know more about this you can have a look at the following video tutorial:-
E&ICT MNIT - Data Science and Machine Learning |
IIT Madras - Advance Certification in Data Science and AI |
E&ICT IIT Guwahati - Cloud Computing & DevOps |
E&ICT IIT Guwahati - Cyber Security |
E&ICT IIT Guwahati - Software Engineering & Application Development |
E&ICT IIT Guwahati - Big Data Analytics |
E&ICT IIT Guwahati - Full Stack Web Development |
E&ICT MNIT - AI and Machine Learning |
E&ICT IIT Guwahati - UI UX Design Strategy |
IIT Madras - Data Analytics for Business |
E&ICT IIT Roorkee - Cloud Computing & DevOps |
E&ICT MNIT - Cyber Security & Ethical Hacking |
E&ICT MNIT - Business Analyst & Project Management
Big Data Analytics Courses |
Data Science Courses |
Business Intelligence courses |
Best Salesforce Courses |
Cloud Computing Courses |
Best Digital Marketing Courses |
Programming Courses Online |
Database Certification Courses |
Project Management Certification Courses |
Best Web Development Certification courses |
Automation Trainings |
UI UX Course
Data Science Courses |
Best Artificial Certification Intelligence Course |
Machine Learning Training |
Python Course |
DevOps Certification Course |
AWS Training and Certification |
Azure Certification |
AWS DevOps Training |
Azure DevOps Certification Course |
Cyber Security Course |
Ethical Hacking Course |
Big Data Hadoop Certification |
SQL Course |
Salesforce Admin Certification |
Online Salesforce Developer Certification |
Selenium Training Online |
Business Analyst Course Online |
Data Analyst Certification course |
Best Microsoft Power BI Certification Course |
Tableau Course Online |
Linux certification Course |
Data Analytics Certification Course |
Digital Marketing Course |
Online Business Analytics Course |
Online Investment Banking Course |
Online Supply Chain Course |
Online Electric Vehicle Course
M. Tech in AI & ML |
M. Sc. in Data Science |
MBA in Big Data Management |
M. Sc. in Artificial Intelligence |
MBA in Business Administration |
MBA in International Marketing |
Master’s in Computer Science |
MBA in Finance and Accounting |
Master’s in Engineering Management |
Master of Science in Data Science |
MSC in Data Science
© COPYRIGHT 2011-2023 INTELLIPAAT.COM. ALL RIGHTS RESERVED.
Download Salary Trends Now !
Learn how professionals like you got up to 100% Salary Hike.