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
  • 7,642
  • 11
  • 35
  • 72
Inazuma
  • 158
  • 3
  • 13

2 Answers2

1

One obvious/intuitive way might be:

people = [person for person in people if person.name != 'Bob']
PascalVKooten
  • 19,461
  • 15
  • 93
  • 149
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
  • 168,988
  • 22
  • 228
  • 312