How to extract from a list of objects a list of specific attribute?

A list comprehension would work just fine:

[o.my_attr for o in my_list]

But there is a combination of built-in functions, since you ask 🙂

from operator import attrgetter
map(attrgetter('my_attr'), my_list)

Leave a Comment