469,362 Members | 2,450 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,362 developers. It's quick & easy.

loop over list and modify in place

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[i].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.
Sep 30 '06 #1
5 10152
Daniel Nogradi wrote:
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[i].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:

pyclass B(object):
.... def __repr__(self):
.... return '<B>: %s' % self.value
.... def __init__(self):
.... self.value = None
....
pyalist = [B() for i in xrange(5)]
pyalist
[<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]
pyalist
[<B>: 5, <B>: 6, <B>: 7, <B>: 8, <B>: 9]
pymap(setattr, alist, ['value']*5, xrange(5))
[None, None, None, None, None]
pyalist
[<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/
Sep 30 '06 #2

James Stroud wrote:
Daniel Nogradi wrote:
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[i].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?

Sep 30 '06 #3
John Machin wrote:
James Stroud wrote:
>>Daniel Nogradi wrote:
>>>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[i].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/
Sep 30 '06 #4
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.
:)
Sep 30 '06 #5
"Daniel Nogradi" <no*****@gmail.comwrites:
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[i].newattribute = 'new value'
for m in mylist:
m.newattribute = 'new value'
Sep 30 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Peter Ballard | last post: by
5 posts views Thread by flupke | last post: by
3 posts views Thread by ken.boss | last post: by
5 posts views Thread by Jani Yusef | last post: by
65 posts views Thread by Steven Watanabe | last post: by
12 posts views Thread by Mark E. Fenner | last post: by
7 posts views Thread by Ivan Voras | last post: by
8 posts views Thread by malkarouri | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.