One of the most noticeable changes from Python 2 to Python 3 is that map, filter, zip and range builtins now return iterators instead of lists. This is usually a good feature to have, because, well, nobody uses range outside of a
The most obvious one is this:
I mean, there are some more exotic ways to do it.
for
statement. But there may be rare cases when resulting list might take less memory then the iterator itself, especially when you nesting them like this:def increase(self, *increments):
self.values = map(sum, zip(self.values, increments))
This is going to eat all the memory and crash with a several gigabyte core dump. So the question is: how does one expand an iterator into a sequence in Python 3? Well, I don't want to hurt the Zen of Python, but there are more than one way to do so.The most obvious one is this:
def increase(self, *increments):
self.values = tuple(map(sum, zip(self.values, increments)))
It's too obvious to write the whole post about it. It is as obvious as to get rid of the iterator in favor of the list comprehension:def increase(self, *increments):
self.values = [sum(x) for x in zip(self.values, increments)]
(Don't forget to use square brackets, or else you will get a generator, which is practically the same as iterator in this case).I mean, there are some more exotic ways to do it.