return reverse(list[ :]list[: def ireverse(list)res list[: for in range(len(list))res list[ : + res return res add front item on the end emptyof same type add each item to front the combination of the changes allows the new functions to work on any sequenceand return new sequence of the same type as the sequence passed in if we pass in stringwe get new string as the result in factthey reverse any sequence object that responds to slicingconcatenationand len--even instances of python classes and types in other wordsthey can reverse any object that has sequence interface protocols here they are working on listsstringsand tuplesfrom rev import reverse([ ])ireverse([ ]([ ][ ]reverse("spam")ireverse("spam"('maps''maps'reverse(( ))ireverse(( )(( )( )implementing sorts another staple of many systems is sortingordering items in collection according to some constraint the script in example - defines simple sort routine in pythonwhich orders list of objects on field because python indexing is genericthe field can be an index or key--this function can sort lists of either sequences or mappings example - pp \dstruct\classics\sort py def sort(listfield)res [for in listi for in resif [field< [field]break + res[ : [xreturn res always returns list list node goes hereinsert in result slot if __name__ ='__main__'table {'name':'john''age': }{'name':'doe''age': print(sort(table'name')print(sort(table'age')table ('john' )('doe' print(sort(table )print(sort(table )reversing and sorting sequences