By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,345 Members | 1,746 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,345 IT Pros & Developers. It's quick & easy.

loop over list and modify in place

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a

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

P: n/a
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

P: n/a
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

P: n/a
"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.