472,374 Members | 1,263 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,374 software developers and data experts.

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 10262
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Peter Ballard | last post by:
Whew. I hope that title is descriptive! Hi all, The python tutorial tells me "It is not safe to modify the sequence being iterated over in the loop". But what if my list elements are mutable,...
5
by: flupke | last post by:
Hi, i'm having trouble with deleting elements from a list in a for loop ============== test program ============== el = print "**** Start ****" print "List = %s " % el index = 0 for line...
3
by: ken.boss | last post by:
I am a python newbie, and am grappling with a fundamental concept. I want to modify a bunch of variables in place. Consider the following: >>> a = 'one' >>> b = 'two' >>> c = 'three' >>>...
5
by: Jani Yusef | last post by:
Based on an interview question I heard of but did not know the answer to....... How do you find and remove a loop from a singly linked list? In a google groups search I found the following code...
65
by: Steven Watanabe | last post by:
I know that the standard idioms for clearing a list are: (1) mylist = (2) del mylist I guess I'm not in the "slicing frame of mind", as someone put it, but can someone explain what the...
12
by: Mark E. Fenner | last post by:
Hello all, I have a code where my inner loop looks like: allNew = for params in cases: newObj = copy(initialObject) newObj.modify(params) allNew.append(newObj) return allNew
7
by: Ivan Voras | last post by:
For a declaration like: List<MyTypeitems = ... where MyType is a struct, attempt to modify an item with items.member = something; fails with the message:
9
by: Andreas Schmitt | last post by:
I am somewhat new to C# and I ran into a problem in a small program I am writing for teaching myself. I am handling a list ob objects and I want to delete some of them inside a loop like in: ...
8
by: malkarouri | last post by:
Hi everyone, I have an algorithm in which I need to use a loop over a queue on which I push values within the loop, sort of: while not(q.empty()): x = q.get() #process x to get zero or more...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.