Iterating properly in Python
After more than a year of python hacking you take a look at the official Python tutorial and still discover new ways of enhancing your code. Just two ways of saving list constructions on our actual code.
The first one: In our code, we are currently doing all the time something like this:
for key, value in myDictionary.items() : doWhatEverWit(key,value)
This is not that appropiate because it creates the tuple list and then iterates. But if you do:
for key, value in myDictionary.iteritems() : doWhatEverWit(key,value)
You just get fetched keys and values without creating any temporary list.
The other thing our code is full of is constructing a list using list comprehensions :
tempList = [expresionUsing(item) for item in collection] for item in tempList: doWhatEverWith(item)
I knew about list comprehensions and i knew about generator functions . Generator functions are functions can be a iteration source by doing yield instead of return. yield returns a value but keeps the function execution state for the next call. That is a very interesting feature but i never used it because it is harder to create a function that yields than to create the loop itself.
But by reading the tutorial i found a brand new kind of expressions that sum up list comprehensions and generator functions: generator expressions . They have the same syntax than list comprehensions using parethesis instead of brackets, but instead of creating a list they create a source of iteration so that no temp lists are created. Previous code would look like that:
iterable = (expresionUsing(item) for item in collection) for item in iterable: doWhatEverWith(item)
There are plenty of places in the code we generated last year (WiKo , TestFarm and build and utility scripts in CLAM ) where we can apply those two ideas. So i am pretty happy on having discovered that.