import sys print(sys pathx print( * but also much more for instancelist comprehensions and the map built-in function use the same protocol as their for loop cousin when applied to filethey both leverage the file object' iterator automatically to scan line by linefetching an iterator with __iter__ and calling __next__ each time throughuppers [line upper(for line in open('script py')uppers ['import sys\ ''print(sys path)\ '' \ ''print( * )\ 'map(str upperopen('script py')map is itself an iterable in list(map(str upperopen('script py'))['import sys\ ''print(sys path)\ '' \ ''print( * )\ 'we introduced the map call used here briefly in the preceding (and in passing in )it' built-in that applies function call to each item in the passed-in iterable object map is similar to list comprehension but is more limited because it requires function instead of an arbitrary expression it also returns an iterable object itself in python xso we must wrap it in list call to force it to give us all its values at oncemore on this change later in this because maplike the list comprehensionis related to both for loops and functionswe'll also explore both again in and many of python' other built-ins process iterablestoo for examplesorted sorts items in an iterablezip combines items from iterablesenumerate pairs items in an iterable with relative positionsfilter selects items for which function is trueand reduce runs pairs of items in an iterable through function all of these accept iterablesand zipenumerateand filter also return an iterable in python xlike map here they are in action running the file' iterator automatically to read line by linesorted(open('script py')['import sys\ ''print(sys path)\ ''print( * )\ '' \ 'list(zip(open('script py')open('script py'))[('import sys\ ''import sys\ ')('print(sys path)\ ''print(sys path)\ ')(' \ '' \ ')('print( * )\ ''print( * )\ ')list(enumerate(open('script py'))[( 'import sys\ ')( 'print(sys path)\ ')( ' \ ')( 'print( * )\ ')list(filter(boolopen('script py'))nonempty=true ['import sys\ ''print(sys path)\ '' \ ''print( * )\ 'import functoolsoperator functools reduce(operator addopen('script py')'import sys\nprint(sys path)\nx \nprint( * )\ iterations and comprehensions