The in
on map
(which is an iterator, not a generator, technically speaking; generators are functions using yield
or generator expressions, and they're a subset of the broader class of iterators) short-circuits as soon as it knows the result to be True
, so it only actually produces and checks six values and then immediately returns True
. The list
comprehension, by contrast, must produce the entire list
of 100 elements before checking any of them.
If your test was for an element that wasn't in the iterable in question, map
's win, if any (the pointless call to int
hurts it, performance-wise), would be smaller, but when the iterable contains the element, and it's early in the iterable, short-circuiting is clearly faster even if each element is more costly to produce, because it produces so many fewer elements.