loop over list and modify in place

D

Daniel Nogradi

Is looping over a list of objects and modifying (adding an attribute
to) each item only possible like this?

mylist = [ obj1, obj2, obj3 ]

for i in xrange( len( mylist ) ):
mylist.newattribute = 'new value'


I'm guessing there is a way to do this without introducing the (in
principle unnecessary) index i, so what I'm really looking for is a
looping method which doesn't pass references to the values of the
items but to the items themselves.
 
J

James Stroud

Daniel said:
Is looping over a list of objects and modifying (adding an attribute
to) each item only possible like this?

mylist = [ obj1, obj2, obj3 ]

for i in xrange( len( mylist ) ):
mylist.newattribute = 'new value'


I'm guessing there is a way to do this without introducing the (in
principle unnecessary) index i, so what I'm really looking for is a
looping method which doesn't pass references to the values of the
items but to the items themselves.


You can use map, or if you don't map, like list comprehension:

py> class B(object):
.... def __repr__(self):
.... return '<B>: %s' % self.value
.... def __init__(self):
.... self.value = None
....
py> alist = [B() for i in xrange(5)]
py> alist
[<B>: None, <B>: None, <B>: None, <B>: None, <B>: None]
py> [setattr(b,'value',v+5) for (v,b) in enumerate(alist)]
[None, None, None, None, None]
py> alist
[<B>: 5, <B>: 6, <B>: 7, <B>: 8, <B>: 9]
py> map(setattr, alist, ['value']*5, xrange(5))
[None, None, None, None, None]
py> alist
[<B>: 0, <B>: 1, <B>: 2, <B>: 3, <B>: 4]

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
 
J

John Machin

James said:
Daniel said:
Is looping over a list of objects and modifying (adding an attribute
to) each item only possible like this?

mylist = [ obj1, obj2, obj3 ]

for i in xrange( len( mylist ) ):
mylist.newattribute = 'new value'


I'm guessing there is a way to do this without introducing the (in
principle unnecessary) index i, so what I'm really looking for is a
looping method which doesn't pass references to the values of the
items but to the items themselves.


You can use map, or if you don't map, like list comprehension:


Call me crazy, but isn't the simple construct
for obj in mylist:
obj.newattribute = 'new value'
what the OP was looking for?
 
J

James Stroud

John said:
James said:
Daniel said:
Is looping over a list of objects and modifying (adding an attribute
to) each item only possible like this?

mylist = [ obj1, obj2, obj3 ]

for i in xrange( len( mylist ) ):
mylist.newattribute = 'new value'


I'm guessing there is a way to do this without introducing the (in
principle unnecessary) index i, so what I'm really looking for is a
looping method which doesn't pass references to the values of the
items but to the items themselves.


You can use map, or if you don't map, like list comprehension:



Call me crazy, but isn't the simple construct
for obj in mylist:
obj.newattribute = 'new value'
what the OP was looking for?


I thought he wanted a one liner.


--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
 
D

Daniel Nogradi

Call me crazy, but isn't the simple construct
for obj in mylist:
obj.newattribute = 'new value'
what the OP was looking for?

Yes, of course. That's why my follow-up post was this:
Please consider the previous question as an arbitrary random brain
cell fluctuation whose probability of occurence is around once per
month and before sending the question it hasn't yet happened to me in
September.

:)
 
P

Paul Rubin

Daniel Nogradi said:
Is looping over a list of objects and modifying (adding an attribute
to) each item only possible like this?

mylist = [ obj1, obj2, obj3 ]

for i in xrange( len( mylist ) ):
mylist.newattribute = 'new value'


for m in mylist:
m.newattribute = 'new value'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top