List constants are surrounded by square brackets and the elements in the list are separated by commas. The individual elements of the list can be accessed using the square brackets by specifying the item's index. The first element is at index 0. Indexes range from 0 to n-1 (where n is the length of the list) and from -1 to -n.
Lists are heterogeneous, i.e., values can be of any type (strings are homogeneous because their elements are characters).
The following is an example of a list and how we can access elements of the list.
1>>> vals = [1, 2, 3, 4, 5, 6] # Define a new list
2>>> vals # Print List
3[1, 2, 3, 4, 5, 6]
4>>> vals[0] # Print first item in list
51
6>>> vals[3] # print fourth item in list
74
8>>> i = 2 # Create a Variable
9>>> vals[2] # Pass the index value using the variable
103
11>>>
12
An index value of ā1 is used to reference the last item of a list and an index value of ā2 is used to reference the previous to last item of the list. Examples below:
1>>> vals[-1] # Access last item of the list
26
3>>> vals[-2] # Access second last item of the list
45
5>>>
6
Lists are Mutable
Lists are mutable, which means that the items of the list can change value by performing assignment on them. In the following example, we assign a new value of 12 to item at index 3.
1>>> vals
2[1, 2, 3, 4, 5, 6]
3>>> vals[3] = 12 # Assign new value to item at index 3
4>>> vals # Notice the change in value of vals[3]
5[1, 2, 3, 12, 5, 6]
6>>>
7
Slicing Operations
The slicing operations are used to access a sublist of the list. The colon notation is used to specify the range of index values of the items. The first index value is written before the colon and the last index value is written after the colon. This indicates the range of index values from the start index value (inclusive) up to but not including the last index value (exclusive) specified.
In the following example, we first define a list calledĀ mylist
Ā which contains 6 elements. The second command specifies a sublist of listĀ mylist
, that includes the items starting with index value 0 up to but not including the item with index value 4. The third command assigns the sublist mylist[2:5] to variableĀ mysublist
.
1>>> mylist = [1,2,3,4,5,6]
2>>> mylist[0:4]
3[1, 2, 3, 4]
4>>> mysublist = mylist[2:5]
5>>> mysublist
6[3, 4, 5]
7
In a list, we can update a range of items using slicing and assignment. For example, the following command changes the values of items with index 0 and up to but not including the item with index value 2 with another set of values.
1>>> mylist[0:2]=mylist[1:3]
2>>> mylist
3[2, 3, 3, 4, 5, 6]
4
Using slicing, we can leave the second index value which implies that the range will be upto the last item. Similarly, we can leave the first index value which implies that the range of items starts with the first item of the list.
1>>> mylist[2:]
2[3, 4, 5, 6]
3>>> mylist[:4]
4[2, 3, 3, 4]
5
Sometimes you may need to count the number of items in a list. This can be done using theĀ len()
Ā method as shown below:
1>>> len('mylist')
26
3>>>
4
Just like theĀ len()
Ā method, all Python objects have many methods attached to them which allow us to perform various actions on the objects. We will learn about these methods in the coming lessons.