You can use list comprehension , as @Matt suggested. you can also use itertools - more precisely, the imap() function:
>>> from itertools import imap >>> from operator import sub >>> a = [3,5,6] >>> b = [3,7,2] >>> imap(int.__sub__, a, b) <itertools.imap object at 0x50e1b0> >>> for i in imap(int.__sub__, a, b): ... print i ... 0 -2 4
Like all itertools funcitons, imap() returns an iterator. You can generate a list passing it as an argument to the list() constructor:
>>> list(imap(int.__sub__, a, b)) [0, -2, 4] >>> list(imap(lambda m, n: mn, a, b)) # Using lambda [0, -2, 4]
EDIT . As suggested in @Cat below, it would be better to use the operator.sub() function with imap() :
>>> from operator import sub >>> list(imap(sub, a, b)) [0, -2, 4]
brandizzi
source share