Asdfg
Asdfg

Reputation: 12203

compare List of attributes against an object inside list

This is how my Input looks like:

cust1Attributes = [{'Key': 'FirstName', 'Value': 'SomeFirstName'}, {'Key': 'LastName', 'Value': 'SomeLastName'}]

This is how my MandatoryData list looks like:

class MustHaveData(object):
    def __init__(self, name, defaultValue):
        self.name = name
        self.defaultValue = defaultValue

customerMandatoryData=[]
customerMandatoryData.append(MustHaveData(name="FirstName", defaultValue="Default First Name"))
customerMandatoryData.append(MustHaveData(name="LastName", defaultValue="Default Last Name"))
customerMandatoryData.append(MustHaveData(name="State", defaultValue="Default State"))

I need to compare cust1Attributes's key against customerMandatoryData's name and get the list of customerMandatoryData back which does not exist for cust1Attributes

How do i do this?

Upvotes: 0

Views: 43

Answers (1)

Moses Koledoye
Moses Koledoye

Reputation: 78546

Build a set from the dictionary containing the items at each Key and use a list comprehension to filter out objects with names in the set:

custset = {x['Key'] for x in cust1Attributes}
result = [obj for obj in customerMandatoryData if obj.name not in custset]

Upvotes: 2

Related Questions