0

I'm not sure if this is possible, but if anyone has any workarounds, that would be great.

If have a class like so:

class person:
    def __init__(self, name):
        self.name = name

And I add a bunch of instances of this class to a list, like so:

people = [person("Bob"), person("Tim"), person("Sue")]

I would, in a perfect world like to then perform this code:

people.remove(person("Bob"))

However, unfortunately, due to the way classes work, this is not possible. Is there some sort of workaround for this? Preferably something that fits onto one line?

Any ideas welcome!

Himanshu Mishra
  • 8,510
  • 12
  • 37
  • 74
Inazuma
  • 178
  • 3
  • 13

2 Answers2

1

One obvious/intuitive way might be:

people = [person for person in people if person.name != 'Bob']
PascalVKooten
  • 20,643
  • 17
  • 103
  • 160
0

If the names are unique:

people = [person("Bob"), person("Tim"), person("Sue")]


people[:] = [ins for ins in people if ins.name != "Bob"]
print(people)
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321