res append( yres [ although list comprehensions construct list resultsremember that they can iterate over any sequence or other iterable type here' similar bit of code that traverses strings instead of lists of numbersand so collects concatenation results[ for in 'spamfor in 'spam'['ss''sp''sa''sm''ps''pp''pa''pm''as''ap''aa''am''ms''mp''ma''mm'each for clause can have an associated if filterno matter how deeply the loops are nested--though use cases for the following sort of codeapart from perhaps multidimensional arraysstart to become more and more difficult to imagine at this level[ for in 'spamif in 'smfor in 'spamif in (' '' ')['sp''sa''mp''ma'[ for in 'spamif in 'smfor in 'spamif in (' '' 'for in ' if ' '['sp ''sp ''sa ''sa ''mp ''mp ''ma ''ma 'finallyhere is similar list comprehension that illustrates the effect of attached if selections on nested for clauses applied to numeric objects rather than strings[(xyfor in range( if = for in range( if = [( )( )( )( )( )( )this expression combines even numbers from through with odd numbers from through the if clauses filter out items in each iteration here is the equivalent statement-based coderes [for in range( )if = for in range( )if = res append((xy)res [( )( )( )( )( )( )recall that if you're confused about what complex list comprehension doesyou can always nest the list comprehension' for and if clauses inside each other like this-indenting each clause successively further to the right--to derive the equivalent statements the result is longerbut perhaps clearer in intent to some human readers on first glanceespecially those more familiar with basic statements list comprehensions and functional tools