4 ms·
> `[x^2 for x in l if x < 10]` is EXACTLY the same as `map(lambda x:x^2, filter(lambda x: x<10, l))` Except the former resolves to an actual list value, wherea
by DonaldPShimoda 8y ago
> `[x^2 for x in l if x < 10]` is EXACTLY the same as `map(lambda x:x^2, filter(lambda x: x<10, l))`
Except the former resolves to an actual list value, whereas the latter is a generator. (You would've been right if you'd used the generator syntax () instead of the list comprehension syntax []. Just thought I'd point it out since this whole thread is about syntax.)
- asdfologist 8y agoThat's true for only python 3.
- DonaldPShimoda 8y agoThe other commenter didn't specify, which led me to believe it was a (simple) mistake on their part. You're not wrong, but it seems to me that dragging out this phase of "Well Python 2 had X but Python 3 has Y" will not help people move to all being on 3. (And, with some exceptions, everyone should really be moving to 3.)
- Sohcahtoa82 8y ago> Except the former resolves to an actual list value, whereas the latter is a generator. The latter is an actual list value if you're still using Python 2: pi@raspberrypi ~ $ python Python 2.7.3 (default, Nov 24 2017, 21:13:24) [GCC 4.6.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> map(lambda x: x^2, filter(lambda x: x < 10, [1,2,3])) [3, 0, 1] >>> pi@raspberrypi ~ $ python3 Python 3.2.3 (default, Mar 25 2017, 13:24:50) [GCC 4.6.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> map(lambda x: x^2, filter(lambda x: x < 10, [1,2,3])) <map object at 0x76a72570>
- DonaldPShimoda 8y agoRight, you're quite correct! I assumed OP was using Python 3 since that's the current version, and their statement would have been true in 3 with the small change I suggested. Their point is still correct overall, but if they're going to make an example from an older version of the language I think they ought to specify as much.
- garmaine 8y agoPython 3 is a different language :)