How to get selected attributes of an object in to a python list?
<p>How do I create a list with the selected attributes of an object in python ? Using list comprehensions.</p>

<p>E.g: </p>

<p>My object A has</p>

<pre><code>A.name
A.age
A.height
</code></pre>

<p>and many more attributes</p>

<p>How do I create a list <code>[name,age]</code></p>

<p>I can do it manually but it looks ugly:</p>

<pre><code>l=[]
l.append(A.name)
l.append(A.age)
</code></pre>

<p>but I am looking for a shortcut.</p>

<p>Why not just <code>[A.name, A.age]</code>? <code>list</code> literals are simple. <a href="https://docs.python.org/3/library/operator.html#operator.attrgetter">You could use <code>operator.attrgetter</code> if you need to do it a lot</a>, though it returns <code>tuple</code>s when fetching multiple attributes, not <code>list</code>s, so you'd have to convert if you can't live with that.</p>

python list list-comprehension python-2.x