List in python is implemented to store the sequence of various type of data. However, python contains six data types that are capable to store the sequences but the most common and reliable type is list.
A list can be defined as a collection of values or items of different types. The items in the list are separated with the comma (,) and enclosed with the square brackets .
A list can be defined as follows.
- L1 = ["John", 102, "USA"]
- L2 = [1, 2, 3, 4, 5, 6]
- L3 = [1, "Ryan"]
List indexing and splitting
The indexing are processed in the same way as it happens with the strings. The elements of the list can be accessed by using the slice operator .
The index starts from 0 and goes to length - 1. The first element of the list is stored at the 0th index, the second element of the list is stored at the 1st index, and so on.
Consider the following example.
Unlike other languages, python provides us the flexibility to use the negative indexing also. The negative indices are counted from the right. The last element (right most) of the list has the index -1, its adjacent left element is present at the index -2 and so on until the left most element is encountered.
Updating List values