orig [: sort(in this example you could also use the built-in function sortedwhich returns newsorted list and leaves the original alone but in that case you should avoid using sorted as variable name listssplitand files when we read and parse filesthere are many opportunities to encounter input that can crash our program so it is good idea to revisit the guardian pattern when it comes writing programs that read through file and look for "needle in the haystacklet' revisit our program that is looking for the day of the week on the from lines of our filefrom stephen marquard@uct ac za sat jan : : since we are breaking this line into wordswe could dispense with the use of startswith and simply look at the first word of the line to determine if we are interested in the line at all we can use continue to skip lines that don' have "fromas the first word as followsfhand open('mbox-short txt'for line in fhandwords line split(if words[ !'fromcontinue print(words[ ]this looks much simpler and we don' even need to do the rstrip to remove the newline at the end of the file but is it betterpython search py sat traceback (most recent call last)file "search py"line in if words[ !'fromcontinue indexerrorlist index out of range it kind of works and we see the day from the first line (sat)but then the program fails with traceback error what went wrongwhat messed-up data caused our elegantcleverand very pythonic program to failyou could stare at it for long time and puzzle through it or ask someone for helpbut the quicker and smarter approach is to add print statement the best place to add the print statement is right before the line where the program failed and print out the data that seems to be causing the failure now this approach may generate lot of lines of outputbut at least you will immediately have some clue as to the problem at hand so we add print of the variable words right before line five we even add prefix "debug:to the line so we can keep our regular output separate from our debug output