[Tutor] Appending to many lists with list comprehension
Bernard Lebel
3dbernard at gmail.com
Tue May 3 05:57:24 CEST 2005
Hello,
I have an object, and this object has attributes. These attributes are
objects in their own right, and each attribute also have its own attributes.
So I loop over the "top" object, and for each attribute encountered, I
want to put in a list two of the attributes of this attribute. Hope it
makes sense.
Right now, here is what I'm doing:
aList = [ [], [] ]
# Iterate attributes of the top object
for oAttribute in oObj.attributes:
# Append to list 0 the attribute "type" of the current attribute
aList[0].append( str(oAttribute.type) )
# Append to list 1 the attribute "name" of the current attribute
aList[1].append( str(oAttribute.name) )
Instead of having two separate lists and a for loop, I'd like to perform
a list comprehension that would do this all in one go, if this is doable.
Thanks
Bernard
More information about the Tutor
mailing list