Python Forum
How to check if class instance exists in a list of class instance objects?
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to check if class instance exists in a list of class instance objects?
#3
You need to implement some special methods in your class. Here is a quick example

class Person():
    def __init__(self, name):
        self.name = name
        
    def __eq__(self, other):
        return (self.name, ) == (other.name, )
        
    def __hash__(self):
        return hash((self.name,))
        
    def __str__(self):
        return self.name
        

    
person1 = Person('John')
person2 = Person('Allice')
person3 = Person('John')

persons = [person1, person2, person3]
print(persons)
for person in persons:
    print(person)

unique_persons = set(persons)
print(unique_persons)
for person in unique_persons:
    print(person)
Output:
[<__main__.Person instance at 0x02FB1B20>, <__main__.Person instance at 0x02FB53F0>, <__main__.Person instance at 0x02FB5418>] John Allice John set([<__main__.Person instance at 0x02FB1B20>, <__main__.Person instance at 0x02FB53F0>]) John Allice >>>
note that I have added __str__() just for more clarity in the example output
if for some reason you don't want/are not able to change the class

class Person():
    def __init__(self, name):
        self.name = name
         
    def __str__(self):
        return self.name
     
person1 = Person('John')
person2 = Person('Allice')
person3 = Person('John')
 
persons = [person1, person2, person3]
print(persons)
for person in persons:
    print(person)
 
def dedup(persons):
    unique = []
    unique_names = set()
    for person in persons:
        if person.name not in unique_names:
            unique.append(person)
            unique_names.add(person.name)
    return unique
    
unique_persons = dedup(persons)
print(unique_persons)
for person in unique_persons:
    print(person)
Output:
[<__main__.Person instance at 0x02FD1E40>, <__main__.Person instance at 0x02FD53A0>, <__main__.Person instance at 0x02FD53C8>] John Allice John [<__main__.Person instance at 0x02FD1E40>, <__main__.Person instance at 0x02FD53A0>] John Allice >>>
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Messages In This Thread
RE: How to check if class instance exists in a list of class instance objects? - by buran - May-25-2018, 11:46 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] Class and methods ebn852_pan 15 732 May-23-2024, 11:57 PM
Last Post: ebn852_pan
  [SOLVED] [listbox] Feed it with dict passed to class? Winfried 3 289 May-13-2024, 05:57 AM
Last Post: Larz60+
  Class and methods Saida2024 2 282 May-13-2024, 04:04 AM
Last Post: deanhystad
  How does this code create a class? Pedroski55 6 662 Apr-21-2024, 06:15 AM
Last Post: Gribouillis
  class definition and problem with a method HerrAyas 2 351 Apr-01-2024, 03:34 PM
Last Post: HerrAyas
  Printing out incidence values for Class Object SquderDragon 3 402 Apr-01-2024, 07:52 AM
Last Post: SquderDragon
  class and runtime akbarza 4 491 Mar-16-2024, 01:32 PM
Last Post: deanhystad
  Help with to check an Input list data with a data read from an external source sacharyya 3 550 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  Operation result class SirDonkey 6 655 Feb-25-2024, 10:53 AM
Last Post: Gribouillis
  The function of double underscore back and front in a class function name? Pedroski55 9 846 Feb-19-2024, 03:51 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020