In Python programming, one can access list elements by passing the index of the list in brackets as integer values (zero-indexed: starting from zero to access the first element of the list and so on). Unlike some other languages, Python allows the use of negative index values to access elements of the list from left-to-right.
So, when you make use of list[-1] you can access the last element of the list.
# Examples list[-1]
python_list = [1, 2, 3, 4, 5, 6, 7]
last_element = python_list[-1]
print(last_element)
Output:
Let's take another example with a list of string values.
![Python list[-1] example Python list[-1] example](https://code2care.org/python/what-is-list-1-in-python-programming/images/Python list[-1] example.jpg)
-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Python,
- Python: Convert Date to DateTime
- How to sort a List using Lambda in Python
- Python matplotlib segmentation fault: 11 macOS Big Sur
- What is Terminal Velocity and its Formula? How to calculate it programmatically?
- How to install Python 3.11 on Mac
- How to flatten a nested list in Python
- Python: Pandas Merge DataFrames on Index Example
- How to Run all Cells at Once Jupyter Notebook
- Python - Convert float to String
- How to add borders to tkinter label text
- How to Exit a Loop in Python Code
- [Python] Fix: ValueError: All arrays must be of the same length
- Sorting an array using Bubble Sort in Python Programming
- How to Unzip a file using Python
- Python: Merge DataFrames Pandas Outer Join Example
- Change label (text) color in tkinter
- Convert Float to String in Python
- Fix: fatal error: No such file or directory compilation terminated
- Python: Access index/counter of a for loop iteration
- Import Other Python Files Examples
- How to install Anaconda on Mac (M1/M2 Mac)
- Python Regular Expression to Find All Matches in List
- How to Read a binary File with Python
- How to disable warnings while Python file execution
- Know current Python Version
More Posts:
- SharePoint 2016 error - Could not find file ManageUserProfileServiceApplicationTenantSimplified.xml - SharePoint
- Error code - 7: There's a more permanent way to sign in to Microsoft Teams - Teams
- Sublime Text 3 Convert Case to Upper, Lower, Title or Swap - Sublime-Text
- Convert Multidimensional Array toString In Java - Java
- PHP header location function not called - PHP
- Show battery percentage on MacBook Menu Bar [Ventura 13] - MacOS
- Test internet speed using macOS Terminal command - MacOS
- iPhone Message: A new iOS update is now available. Please update from the iOS 14 beta. - Apple