by naiquevin on 5/25/13, 10:17 AM with 28 comments
by wting on 5/25/13, 1:38 PM
Save yourself time and effort down the road and read through both libraries' documentation, they're well worth the effort:
http://docs.python.org/3.3/library/itertools.html
http://docs.python.org/3.3/library/collections.html
I tend to use defaultdict, deque (thread safe), namedtuple, imap, izip, drop/takewhile. In Python 3, map and zip have been replaced with their itertools equivalents.
I blame Haskell for all the lazy evaluation influence. :P
by masklinn on 5/25/13, 3:06 PM
def flatmap(f, items):
return itertools.chain(*map(f, items))
1. in Python 2 `map` is eager which — as with the previous `even` filter — may lead to unnecessary work if you only need part of the list (or a dead process if the input is infinite...). itertools.imap (or a generator comprehension) would be better. This is "fixed" in Python 3 (where the `map` builtin has become lazy and `itertools.imap` has been removed) but2. it's being eagerly unpacked through *, itertools.chain also provides a from_iterable method which doesn't have that issue (and can be used to flatten infinite streams), introduced in 2.6
So `flatmap` would probably be better as:
def flatmap(f, items):
return itertools.chain.from_iterable(
itertools.imap(
f, items))
by serjeem on 5/25/13, 4:28 PM
by davvolun on 6/3/13, 4:27 PM