Don't forget to also checkout my second blog containing articles to all other related ICT topics!!

Thursday, May 3, 2012

Itertools imap method explained

Itertools imap makes an iterator that computes the function using arguments from each of the iterables. If function is set to None, then imap() returns the arguments as a tuple. Like map() but stops when the shortest iterable is exhausted instead of filling in None for shorter iterables.
from itertools import imap

numbers1 = [0,1,6,8,9,13]
numbers2 = [4,6,2,1,5,11]

def add(a,b):
    return a + b

for result in imap(add, numbers1, numbers2):
    print result

4
7
8
9
14
24

No comments:

Post a Comment