I have a list of custom Python objects and need to search for the existence of specific objects within that list. My concern is the performance implications of searching large lists, especially frequently.
Here's a simplified example using a custom Person class with attributes name and age:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
people = [Person("Alice", 30), Person("Bob", 25), Person("Charlie", 35)]
Currently, I'm using a list comprehension and the any() function to check if a person with a specific name and age exists in the list:
if any(p.name == "Bob" and p.age == 25 for p in people):
print("The person exists.")
Is there a more efficient way to search for the existence of specific custom objects within large Python lists?