localize names as in xc:\codepy - ( for in range( )at ee nameerrorname 'xis not defined [ for in range( )[ for in range( )pass xlist does not localize its nameslike for for loops do not localize names in or if you care about version portabilityand symmetry with the for loop statementuse unique names for variables in comprehension expressions as rule of thumb the behavior makes sense given that generator object is discarded after it finishes producing resultsbut list comprehension is equivalent to for loop--though this analogy doesn' hold for the set and dictionary forms that localize their names in both pythonsand aresomewhat coincidentallythe topic of the next section comprehending set and dictionary comprehensions in senseset and dictionary comprehensions are just syntactic sugar for passing generator expressions to the type names because both accept any iterablea generator works well here{ for in range( ){ set( for in range( ){ comprehension generator and type name {xx for in range( ){ dict((xx xfor in range( ){ nameerrorname 'xis not defined loop variable localized in as for list comprehensionsthoughwe can always build the result objects with manual codetoo here are statement-based equivalents of the last two comprehensions (though they differ in that name localization)res set(for in range( ) comprehensions and generations set comprehension equivalent