In Python lists allows us to store a sequence of items in a single variable.
We create a list by placing elements inside square brackets [], separated by commas. For example,
Lists are:
Each element in a list is associated with a number, known as a index.
The index always starts from 0. The first element of a list is at index 0, the second element is at index 1, and so on.
Negative Indexing in Python
Python also supports negative indexing. The index of the last element is -1, the second-last element is -2, and so on.
[‘Python’, ‘Swift’, ‘C++’] Index → 0 1 2
Negative Index → -3 -2 -1
Python Negative Indexing
Negative indexing makes it easy to access list items from last.
Let’s see an example,
Slicing of a List in Python
In Python, it is possible to access a section of items from the list using the slicing operator :. For example,