dict and set comprehensions were added to python fairly recently in python and python nested list comprehensions suppose we have list of lists containing some boy and girl namesin [ ]all_data [['tom''billy''jefferson''andrew''wesley''steven''joe']['susie''casey''jill''ana''eva''jennifer''stephanie']you might have gotten these names from couple of files and decided to keep the boy and girl names separate nowsuppose we wanted to get single list containing all names with two or more ' in them we could certainly do this with simple for loopnames_of_interest [for names in all_dataenough_es [name for name in names if name count(' ' names_of_interest extend(enough_esyou can actually wrap this whole operation up in single nested list comprehensionwhich will look likein [ ]result [name for names in all_data for name in names if name count(' '> in [ ]result out[ ]['jefferson''wesley''steven''jennifer''stephanie'at firstnested list comprehensions are bit hard to wrap your head around the for parts of the list comprehension are arranged according to the order of nestingand any filter condition is put at the end as before here is another example where we "flattena list of tuples of integers into simple list of integersin [ ]some_tuples [( )( )( )in [ ]flattened [ for tup in some_tuples for in tupin [ ]flattened out[ ][ keep in mind that the order of the for expressions would be the same if you wrote nested for loop instead of list comprehensionflattened [for tup in some_tuplesfor in tupflattened append(xdata structures and sequences www it-ebooks info