the following code parses log file in the previously presented formatand outputs new log file that contains only the warning linesimport sys inname sys argv[ outname sys argv[ with open(innameas infilewith open(outname" "as outfilewarnings ( for in infile if 'warningin lfor in warningsoutfile write(lthis program takes the two filenames on the command lineuses generator expression to filter out the warnings (in this caseit uses the if syntaxand leaves the line unmodified)and then outputs the warnings to another file if we run it on our sample filethe output looks like thisjan : : serious jan : : jan : : jan : : warning this is warning it could be warning warning warning another warning sent warnings should be heeded watch for warnings of coursewith such short input filewe could have safely used list comprehensionbut if the file is millions of lines longthe generator expression will have huge impact on both memory and speed generator expressions are frequently most useful inside function calls for examplewe can call summinor maxon generator expression instead of listsince these functions process one object at time we're only interested in the resultnot any intermediate container in generala generator expression should be used whenever possible if we don' actually need listsetor dictionarybut simply need to filter or convert items in sequencea generator expression will be most efficient if we need to know the length of listor sort the resultremove duplicatesor create dictionarywe'll have to use the comprehension syntax