473,386 Members | 1,702 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 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 10306
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.