Python: Fetching Last n Items of an Iterable

Fetching Last n Items of an Iterable in Python

Fetching Last n Items of an Iterable: In order to fetch the last n items of an iterable, you can use slicing with a negative index. Slicing is a handy way to quickly access parts of an iterable, and is a very fast operation.

Fetching Last n Items of an Iterable

Say you have a list of 10 numbers added sequentially, and you need to retrieve the most recently added 5 elements, then you would write listOne[-5:] to serve the purpose. This can be read as "5th from end to end."