collection data types python has sorted the list of tuples by comparing the first item of each tupleand when these are the sameby comparing the second item this gives sort order based on the integerswith the strings being tiebreakers we can force the sort to be based on the strings and use the integers as tiebreakers by defining simple key functiondef swap( )return [ ] [ the swap(function takes -tuple and returns new -tuple with the arguments swapped assuming that we have entered the swap(function in idlewe can now do thissorted(xkey=swap[( 'canoe')( 'dorie')( 'kayak')( 'pram')lists can also be sorted in-place using the list sort(methodwhich takes the same optional arguments as sorted(sorting can be applied only to collections where all the items can be compared with each othersorted([ - ]sorted([ "spanner"- ]returns[- raises typeerror although the first list has numbers of different types (int and float)these types can be compared with each other so that sorting list containing them works fine but the second list has string and this cannot be sensibly compared with numberand so typeerror exception is raised if we want to sort list that has integersfloating-point numbersand strings that contain numberswe can give float(as the key functionsorted([" "- " " "- " ]key=floatthis returns the list [- '- ' ' ' ' 'notice that the list' values are not changedso strings remain strings if any of the strings cannot be converted to number ( "spanner") valueerror exception will be raised copying collections object references |since python uses object referenceswhen we use the assignment operator (=)no copying takes place if the right-hand operand is literal such as string or numberthe left-hand operand is set to be an object reference that refers to the in-memory object that holds the literal' value if the right-hand operand is an object referencethe left-hand operand is set to be an object reference that refers to the same object as the right-hand operand one consequence of this is that assignment is very efficient