Messing With Indices
One of the things that makes Python so awesome is its list indexing system (and the fact that this often applies to other things than lists). I know this has been mentioned elsewhere but I havn't seen a proper tut on it yet so here goes.
IMPORTANT - while everything you see in here uses lists as the example datastructure you can use these exact techniques on strings as well (extract single characters/chunks of strings/reverse strings)
As you are probably aware, lists in Python are indexed using square brackets (''), usually with a number inside them. What you may not be aware of is that you can perform much more jiggery pokery than retrieving a single list element using the square brackets, ie
mylist = [0,1,2,3,4,5,6,7,8,9] print 'Indices' print mylist print mylist[-1] print mylist[-2] print '' print 'Ranges' print mylist[:] print mylist[0:2] print mylist[:2] print mylist[-2:] print mylist[5:-1] print '' print 'Complex Ranges' print mylist[::-1] print mylist[8:3:-2] print mylist[5:0:-1] print mylist[5::-1] print mylist[0:5:2] print mylist[-3:5:-1] #OUTPUT: #>>> #Indices #0 #9 #8 # #Ranges #[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] #[0, 1] #[0, 1] #[8, 9] #[5, 6, 7, 8] # #Complex Ranges #[9, 8, 7, 6, 5, 4, 3, 2, 1, 0] #[8, 6, 4] #[5, 4, 3, 2, 1] #[5, 4, 3, 2, 1, 0] #[0, 2, 4] #[7, 6] #>>>
Hopefully you can figure out what's going on here fairly easily, try copying the code into a console of your own and have a play with it.
You can 'reverse index' a list by simply whacking a negative number in the brackets, starting with '-1' for the last entry - think of it as 'mylist[len(mylist)-x]' where 'x' is any integer.
To extract a range of values out you specify the index at which you want to start, add a ':', then specify the EXCLUSIVE index at which you want to finish (ie mylist[4:6] will return mylist and mylist). As you can see from the examples the negative indices will work here as well. If you want to start at index '0' or finish at the last element (inclusive) then you can leave that part of the range expression blank, ie '[0:5]' = '[:5]' and '[5:len(mylist)]' = '[5:]'
To extract a slightly more complicated range of values you can use what is effectively a compact 'for' loop in the brackets using the format [a:b:c] where:
- a is the start index
- b is the exclusive end index
- c is the step size
If you want to iterate backwards (ie reverse the list) then use a negative number for 'c'. You will need to update 'a' and 'b' to reflect this as well, obviously if you are iterating backwards then you will start at a higher index and finish at a lower one, as a general rule:
a > b where c > 0
a < b where c < 0
There is a particular trick you will want to remember with this method and it has particular significance when c < 0. If you have a negative 'c' and you want to move down through your list to the beginning you can't set 'b' to 0 ([4:0:-1]) because that will iterate through your list and IGNORE the '0' index (remember 'b' is an EXCLUSIVE bound). The way you get around this is to leave 'b' blank as we discussed with the ranges scenario above, ie [4::-1]. Python will assume that because 'c' < 0, the blank 'b' should refer to the beginning of the array, not the end and everything should work as expected, returning [3,2,1,0].
That's pretty much the end of this tutorial, I'll try and get the next one out soon where I will be showcasing inline-ifs and will introduce some handy hints for File I/O. Feel free to ask questions below.