
492
Rozdział 12.
Alternatywa dla pętli for
Przyjrzyj się uważnie swojej komprehencji
Przyjrzyjmy się nieco uważniej naszej komprehencji. Poniżej znajdziesz zarówno
oryginalne trzy wiersze kodu, jak i mieszczącą się w jednym wierszu komprehencję,
która wykonuje to samo zadanie.
Pamiętaj, że obydwie wersje tworzą nowe listy (
destinations
oraz
more_dests
)
zawierające dokładnie te same dane.
destinations = []
for dest in flights.values():
destinations.append(dest.title())
more_dests = [dest.title() for dest in flights.values()]
destinations = [ ]
for dest in flights.values():
destinations.append(dest.title())
more_dests = [dest.title() for dest in flights.values() ...