- An index is the position number of an item in a list
- Must be an integer
For example:
>>> spam = ['cat', 'bat', 'rat', 'elephant']
>>> spam[0]
'cat'
>>> spam[1]
'bat'
>>> spam[2]
'rat'
>>> spam[3]
'elephant'
>>> ['cat', 'bat', 'rat', 'elephant'][3]
'elephant'
Negative indexes #
Lists also have negative indexes. You can grab an item from a list like so:
>>> spam = ['cat', 'bat', 'rat', 'elephant']
>>> spam[-2]
'rat'
You’ll notice that negative indexes start at -1 (and not -0!).
Changing values of list items with indexes #
>>> spam = ['cat', 'bat', 'rat', 'elephant']
>>> spam[1] = 'aardvark'
>>> spam
['cat', 'aardvark', 'rat', 'elephant']