Python List

In Python lists allows us to store a sequence of items in a single variable.

Create a Python list

We create a list by placing elements inside square brackets [], separated by commas. For example,

List Characteristics

Lists are:

  • Ordered – They maintain the order of elements.
  • Mutable – Items can be changed after creation.
  • Allow duplicates – They can contain duplicate values.

Access List Elements

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,

Output: