Accessing Class Variables from a List in a nice way in Python
- by Dennis
Suppose I have a list X = [a, b, c] where a, b, c are instances of the same class C.
Now, all these instances a,b,c, have a variable called v, a.v, b.v, c.v ...
I simply want a list Y = [a.v, b.v, c.v]
Is there a nice command to do this?
The best way I can think of is:
Y = []
for i in X
Y.append(i.v)
But it doesn't seem very elegant ~ since this needs to be repeated for any given "v"
Any suggestions? I couldn't figure out a way to use "map" to do this.