I have a list of dataclass instances in the form of:
dataclass_list = [DataEntry(company="Microsoft", users=["Jane Doe", "John Doe"]), DataEntry(company="Google", users=["Bob Whoever"]), DataEntry(company="Microsoft", users=[])]
Now I would like to filter that list and get only unique instances by a certain key (company in this case).
The desired list:
new_list = [DataEntry(company="Microsoft", users=["Jane Doe", "John Doe"]), DataEntry(company="Google", users=["Bob Whoever"])]
The original idea was to use a function in the fashion of python's set() or filter() functions, but both is not possible here.
My working solution so far:
tup_list = [(dataclass, dataclass.company)) for dataclass in dataclass_list]
new_list = []
check_list = []
for tup in tup_list:
if tup[1].lower() not in check_list:
new_list.append(tup[0])
check_list.append(tup[1].lower())
This gives me the desired output but I was wondering whether there is a more pythonic or elegant solution?