Python - Access List Items
Python - Access List Items
Dark mode
Dark code
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP BOOTSTRAP HOW TO W3.CSS C C++ C# REACT R JQUERY DJANGO
Python HOME
Python Intro
Python Get Started
Join Lists
Negative Indexing
Negative indexing means start from the end
-1 refers to the last item, -2 refers to the second last item etc.
Example
Print the last item of the list: COLOR PICKER
Try it Yourself »
Range of Indexes
You can specify a range of indexes by specifying where to start and where to end the range.
When specifying a range, the return value will be a new list with the specified items.
Example
Return the third, fourth, and fifth item:
Try it Yourself »
Note: The search will start at index 2 (included) and end at index 5 (not included).
ADVERTISEMENT
By leaving out the start value, the range will start at the first item:
Example
This example returns the items from the beginning to, but NOT including, "kiwi":
Try it Yourself »
By leaving out the end value, the range will go on to the end of the list:
Example
This example returns the items from "cherry" to the end:
Try it Yourself »
ADVERTISEMENT
Example
This example returns the items from "orange" (-4) to, but NOT including "mango" (-1):
Try it Yourself »
Example
Check if "apple" is present in the list:
Try it Yourself »
ADVERTISEMENT
ADVERTISEMENT
FORUM | ABOUT
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we
cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.