Here are some examples of using indexing to access a single element of an list:
>>> list = ['zero','one','two','three','four','five'] >>> list[0] 'zero' >>> list[4] 'four' >>> list[5] 'five'
len
function like list[len(list)-1]
. This way works since the len
function always returns the last index plus one. The second from the last would then be list[len(list)-2]
. There is an easier way to do this. In Python the last item is always index -1. The second to the last is index -2 and so on. Here are some more examples:
>>> list[len(list)-1] 'five' >>> list[len(list)-2] 'four' >>> list[-1] 'five' >>> list[-2] 'four' >>> list[-6] 'zero'
Another useful way to get into parts of lists is using slices. Here is another example to give you an idea what they can be used for:
>>> list = [0,'Fred',2,'S.P.A.M.','Stocking',42,"Jack","Jill"] >>> list[0] 0 >>> list[7] 'Jill' >>> list[0:8] [0, 'Fred', 2, 'S.P.A.M.', 'Stocking', 42, 'Jack', 'Jill'] >>> list[2:4] [2, 'S.P.A.M.'] >>> list[4:7] ['Stocking', 42, 'Jack'] >>> list[1:5] ['Fred', 2, 'S.P.A.M.', 'Stocking']
list[first_index:following_index]
. The slice goes from the first_index
to the index before the following_index
. You can use both types of indexing:
>>> list[-4:-2] ['Stocking', 42] >>> list[-4] 'Stocking' >>> list[-4:6] ['Stocking', 42]
>>> list[:2] [0, 'Fred'] >>> list[-2:] ['Jack', 'Jill'] >>> list[:3] [0, 'Fred', 2] >>> list[:-5] [0, 'Fred', 2]
poem = ["<B>","Jack","and","Jill","</B>","went","up","the","hill","to","<B>",\ "fetch","a","pail","of","</B>","water.","Jack","fell","<B>","down","and",\ "broke","</B>","his","crown","and","<B>","Jill","came","</B>","tumbling",\ "after"] def get_bolds(list): true = 1 false = 0 ## is_bold tells whether or not the we are currently looking at ## a bold section of text. is_bold = false ## start_block is the index of the start of either an unbolded ## segment of text or a bolded segment. start_block = 0 for index in range(len(list)): ##Handle a starting of bold text if list[index] == "<B>": if is_bold: print "Error: Extra Bold" ##print "Not Bold:",list[start_block:index] is_bold = true start_block = index+1 ##Handle end of bold text if list[index] == "</B>": if not is_bold: print "Error: Extra Close Bold" print "Bold [",start_block,":",index,"] ",\ list[start_block:index] is_bold = false start_block = index+1 get_bolds(poem)
Bold [ 1 : 4 ] ['Jack', 'and', 'Jill'] Bold [ 11 : 15 ] ['fetch', 'a', 'pail', 'of'] Bold [ 20 : 23 ] ['down', 'and', 'broke'] Bold [ 28 : 30 ] ['Jill', 'came']
The get_bold
function takes in a list that is broken into words and token's. The tokens that it looks for are <B>
which starts the bold text and <\B>
which ends bold text. The function get_bold
goes through and searches for the start and end tokens.
The next feature of lists is copying them. If you try something simple like:
>>> a = [1,2,3] >>> b = a >>> print b [1, 2, 3] >>> b[1] = 10 >>> print b [1, 10, 3] >>> print a [1, 10, 3]
b = a
makes b a reference to a. This means that b can be thought of as another name for a. Hence any modification to b changes a as well. However using functions that assign to the whole of b don't modify a:
>>> a = [1,2,3] >>> b = a >>> b = b*2 >>> print a [1, 2, 3] >>> print b [1, 2, 3, 1, 2, 3]
b = b*2
creates a copy. Basically if you have a statement like whole_list_b = whole_list_a
you create a reference. When you pass a list as a argument to a function you create a reference as well. Subsequent modifications to parts of whole_list_b
will modify the original list. Most of the time you don't have to worry about this distinction. However when you need to make a copy of a list you have to make sure that you have actually created a copy.
There are several ways to make a copy of a list. The simplest that works most of the time is the slice operator:
>>> a = [1,2,3] >>> b = a[:] >>> b[1] = 10 >>> print a [1, 2, 3] >>> print b [1, 10, 3]
>>> import copy >>> a = [[1,2,3],[4,5,6]] >>> b = a[:] >>> c = copy.deepcopy(a) >>> b[0][1] = 10 >>> c[1][1] = 12 >>> print a [[1, 10, 3], [4, 5, 6]] >>> print b [[1, 10, 3], [4, 5, 6]] >>> print c [[1, 2, 3], [4, 12, 6]]
By now you are probably wondering why are references used at all? The basic reason is speed. It is much faster to make a reference to a thousand element list than to copy all the elements. Just remember about references if you ever have some weird problem with data being changed when it shouldn't be.