3 ms·
One feature of Python 3 that I found really pleasantly useful recently (thank you SO :) ) was passing generators to the zip() function in a for loop, like: for
by epberry 10y ago
One feature of Python 3 that I found really pleasantly useful recently (thank you SO :) ) was passing generators to the zip() function in a for loop, like:
for a, b in zip(generatorA(), generatorB()):
In Python 2 you have to import itertools to do this. I bring this up because I find examples like this all the time in Python 3 where core, modern language features like generators are more tightly integrated and easier to use. It's really a better version of the language and if you're starting fresh on a Python project you should do yourself a favor and use it.
- mattdeboard 10y agoNaw, `zip` is a builtin in py2. You might be thinking of `itertools.izip`? https://repl.it/E5v6/2 https://repl.it/E5v6/2
- Goopplesoft 10y agozip in py2 returns a tuple type (i.e tries to consumes the iterator through a StopIteration) in py3 it returns a iterator (consumes it on demand). Try this in py2: https://repl.it/E5vd/0 https://repl.it/E5vd/0
- deleted 10y ago[deleted]
- mattdeboard 10y agoI'm not near a real REPL, what's the diff? Eager vs. lazy? I think I remember reading something about that change awhile ago.
- reubano 10y agoExactly. Py3 zip is lazy like py2 izip.