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 .

Recommended Answers

All 2 Replies

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]

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) ]
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.