sion can often make surprisingly large difference in how we reason about code and how easy it is to understand the ternary operator also performs similar restructuring of our focususing the same keywords in different order for exampleif our original code wascollection list(for datum in data_setif condition(datum)collection append(datumelsenew modify(datumcollection append(newsomewhat more compactly we could write this ascollection [ if condition(delse modify(dfor in data_setfar more important than simply saving few characters and lines is the mental shift enacted by thinking of what collection isand by avoiding needing to think about or debug "what is the state of col lection at this point in the loop?list comprehensions have been in python the longestand are in some ways the simplest we now also have generator comprehensionsset comprehensionsand dict comprehensions available in python syntax as caveat thoughwhile you can nest comprehensions to arbitrary depthpast fairly simple level they tend to stop clarifying and start obscuring for genuinely complex construction of data collectionrefactoring into functions remains more readable generators generator comprehensions have the same syntax as list comprehensions--other than that there are no square brackets around them (but parentheses are needed syntactically in some contextsin place of brackets)--but they are also lazy that is to say that they are merely description of "how to get the datathat is not realized until one explicitly asks for iteither by calling next(on the objector by looping over it this often saves memory for large sequences and defers computation until it is actually needed for examplelog_lines (line for line in read_line(huge_log_fileif complex_condition(line)comprehensions