Q: Given a sequence, how do I apply a method to all members of that sequence?

A: Use a list comprehension or a generator expression:

result = [obj.method() for obj in sequence] 

result = (obj.method() for obj in sequence)

(the former generates a list, the latter an iterator that calls the methods as you fetch new items from the iterator).

Alternatively, you can use a helper function:

def method_map(objects, method, arguments):
     """method_map([a,b], "meth", (1,2)) gives [a.meth(1,2), b.meth(1,2)]"""
     nobjects = len(objects)
     methods = map(getattr, objects, [method]*nobjects)
     return map(apply, methods, [arguments]*nobjects)

CATEGORY: programming