it' common when iterating over sequence to want to keep track of the index of the current item do-it-yourself approach would look likei for value in collectiondo something with value + since this is so commonpython has built-in function enumerate which returns sequence of (ivaluetuplesfor ivalue in enumerate(collection)do something with value when indexing dataa useful pattern that uses enumerate is computing dict mapping the values of sequence (which are assumed to be uniqueto their locations in the sequencein [ ]some_list ['foo''bar''baz'in [ ]mapping dict((vifor iv in enumerate(some_list)in [ ]mapping out[ ]{'bar' 'baz' 'foo' sorted the sorted function returns new sorted list from the elements of any sequencein [ ]sorted([ ]out[ ][ in [ ]sorted('horse race'out[ ]['' '' '' '' '' '' '' '' '' ' common pattern for getting sorted list of the unique elements in sequence is to combine sorted with setin [ ]sorted(set('this is just some string')out[ ]['' '' '' '' '' '' '' '' '' '' '' '' 'zip zip "pairsup the elements of number of liststuplesor other sequencesto create list of tuplesin [ ]seq ['foo''bar''baz'in [ ]seq ['one''two''three'in [ ]zip(seq seq out[ ][('foo''one')('bar''two')('baz''three') appendixpython language essentials www it-ebooks info