Q:

Having itemgetter and attrgetter at the same time

I'm trying to pick a most common item among a list based on the element's attribute.
a1 = Myclass(name='a')
a2 = Myclass(name='a')
b1 = Myclass(name='b')
l = [a1,a2,b1]

if most_common(l, attr_name='name') in [a1, a2]:
   # I want this true 
   # 'a' is the most occured 'name'

Basically I want to modify the code https://stackoverflow.com/a/1520716/433570
key = operator.itemgetter(0) then operator.attrgetter(attr_name)
wonder if that's possible?

A:

You can preprocess your list:
attr_getter = operator.attrgetter('name')
names = map(attr_getter, l)
if most_common(names) in set(map(attr_getter, [a1, a2])):
    pass # do smth here

