Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Retrieving the Last Item of a List

my_list = ['red', 'blue', 'green']

# Get the last item with brute force using len
last_item = my_list[len(my_list) - 1]

# Remove the last item from the list using pop
last_item = my_list.pop() 

# Get the last item using negative indices *preferred & quickest method*
last_item = my_list[-1]

# Get the last item using iterable unpacking
*_, last_item = my_list
Comment

PREVIOUS NEXT
Code Example
Python :: remove punctuation and special charaacters nltk 
Python :: how to make a series in python alternating between + and - 
Python :: how to import a all the modules in a packege python 
Python :: mechanize python #2 
Python :: mechanize python XE #25 
Python :: module level import not at top of file 
Python :: Lazada link 
Python :: get users except superuser django 
Python :: append to a list without intializing 
Python :: python dynamic csvnfile joining 
Python :: generate jwt token just passing userid in rest_framework_simplejwt 
Python :: diccionario 
Python :: close window tkiinter 
Python :: python access class property from string 
Python :: python mod of list numpy 
Python :: how to make a new df from old 
Python :: FizzBuzz in Python Using itertools 
Python :: Pyturch training along with source code 
Python :: how to un register DefaultAdminSite in django 
Python :: tuple with mixed data types 
Python :: enumerate zip together 
Python :: testing grepper python 
Python :: vortex core line detection 
Python :: ouvrir fichier txt python et le lire 
Python :: Python NumPy squeeze function Example with axis 
Python :: geopandas nc file 
Python :: Python NumPy asfortranarray Function Scalar to an array 
Python :: button to redirect to another tree view in odoo 
Python :: pandas dt.weekday to string 
Python :: using Canvas with tkinger 
ADD CONTENT
Topic
Content
Source link
Name
6+1 =