| 130 | |
| 131 | === Map and Zip return generators === |
| 132 | |
| 133 | In Python-3, the {{{map()}}} and {{{zip()}}} functions return generator objects rather than lists. Where lists are required, the return value must be converted explicitly using the list constructor. Where possible, we prefer list comprehensions. |
| 134 | {{{ |
| 135 | # This is fine: |
| 136 | for item in map(func, values): |
| 137 | # This could be wrong: |
| 138 | result = map(func, values) |
| 139 | # This is better: |
| 140 | result = list(map(func, values)) |
| 141 | # This could be even better (though not for multiple iterables): |
| 142 | result = [func(v) for v in values] |
| 143 | }}} |