python 2.7 - Compare list or set on part of the JSON object -


this how input looks like:

inputdata=[] inputdata.append({"customername": "customera","state": "statea","itemnumber": "item1"}) inputdata.append({"customername": "customera","state": "statea","itemnumber": "item2"}) inputdata.append({"customername": "customerb","state": "stateb","itemnumber": "item1"}) inputdata.append({"customername": "customerb","state": "stateb","itemnumber": "item2"}) inputdata.append({"customername": "customerx","state": "statex","itemnumber": "item1"}) inputdata.append({"customername": "customerx","state": "statex","itemnumber": "item2"}) 

this list comparing against find out if customer allowed buy item or not.

allowedcustomers = ["custombera","customberb"] 

this how comparing lists:

unauthorizedcustomers = list(set(inputdata)-set(allowedcustomers)) 

how modify above statement comparison happens on customername unauthorizedcustomers list has customerx's full data?

[{"customername": "customerx","state": "statex","itemnumber": "item1"}, {"customername": "customerx","state": "statex","itemnumber": "item2"})] 

>>> inputcustomernames = [ item['customername'] item in inputdata ] # list of input customer names >>> unauthorizedcustomers = list(set(inputcustomernames) - set(allowedcustomers)) # find unauthorized customers >>> unauthorizedcustomersdetails = [ item item in inputdata if item['customername'] in unauthorizedcustomers ] # data of unauthorized customers 

you should read list comprehensions understand happening here. list comprehensions


Comments