0

Hi
I wanted to know how can I delete class object inside list.

class Record:
def __init__(self):

self.list = []

def add(self):
fname = raw_input("First Name:> ")
lname = raw_input("Last Name:> ")
street = raw_input("Street:> ")
self.list.append( Person( fname, lname, street))


Now I have added my Person object into the record list.

My question is how can i delete Person object in the list depending on the person.name

def delete(self):
fname = raw_input("First Name:> ")
for item in self.list:
if (item.name == fname):
del ???????????????

Help me out.
Thanks .

3
Contributors
2
Replies
4
Views
9 Years
Discussion Span
Last Post by katharnakh
0

Why not use code tags so the indentations are preserved in your Python code?

This should work:

def delete(self):
    fname = raw_input("First Name:> ")
    for ix, item in enumerate(self.list):
        if (item.name == fname):
            del self.list[ix]
0

hi,
you can avoid iterating over the loop..

def delete(self):
    fname = raw_input("First Name:>")
    if fname in self.list:
        del self.list[ self.list.index(fname) ]
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.