Accessing Class Variables from a List in a nice way in Python
Posted
by Dennis
on Stack Overflow
See other posts from Stack Overflow
or by Dennis
Published on 2010-03-14T11:35:22Z
Indexed on
2010/03/14
11:45 UTC
Read the original article
Hit count: 352
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.
© Stack Overflow or respective owner