modify element of a list.

K

KraftDiner

Hi I have a list of Ojbects... I want to change one of the objects in
the list for a new object....
How do I replace an existing object with a new one and maintain the
list order..

This is what I have...

def setAttribute(self, desc, value):
n = anObject(desc, value)
for o in self.Objects:
if o.getDescription() == desc:
self.Objects.replace(o, n) #Replace o with n?
return
self.Objects.append(n)

It's the replace in place that I don't know how to do...
 
P

Pierre Quentel

Hi,

The most simple is to use the index of the element in the list :

def setAttribute(self, desc, value):
n = anObject(desc, value)
for i,o in enumerate(self.Objects):
if o.getDescription() == desc:
self.Objects = n
return
self.Objects.append(n)

Pierre
 
S

Steve Holden

KraftDiner said:
Hi I have a list of Ojbects... I want to change one of the objects in
the list for a new object....
How do I replace an existing object with a new one and maintain the
list order..

This is what I have...

def setAttribute(self, desc, value):
n = anObject(desc, value)
for o in self.Objects:
if o.getDescription() == desc:
self.Objects.replace(o, n) #Replace o with n?
return
self.Objects.append(n)

It's the replace in place that I don't know how to do...
There are a number of ways. I suspect the simplest would be (untested):

def setAttribute(self, desc, value):
n = anObject(desc, value)
for i, o in enumerate(self.Objects):
if o.getDescription() == desc:
self.Objects = n
return
self.Objects.append(n)

regards
Steve
 

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
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top