0

I have a list of dictionaries and I am trying to loop and need to check 'pmdruleid' in the rulenames matches any of the 'pmdruleid' in activeclientrulelist. I am using below code and am getting None every time. The below code checks corresponding index value in the loop instead of checking any matching values in list:

activeclientrulelist = [{'pmdruleid': 112, 'createdby': 'Ad hoc Script - PHARMMD\\Jake.Woods', 'updatedby': None, 'transferstatuscode': 1}, 
                        {'pmdruleid': 10160, 'createdby': 'Ad hoc Script - PHARMMD\\Jake.Woods', 'updatedby': None, 'transferstatuscode': 1}, 
                        {'pmdruleid': 10016, 'createdby': 'Ad hoc Script - PHARMMD\\Jake.Woods', 'updatedby': None, 'transferstatuscode': 1}, 
                        {'pmdruleid':111, 'createdby': 'Ad hoc Script - PHARMMD\\Jake.Woods', 'updatedby': None, 'transferstatuscode': 1}, 
                        {'pmdruleid': 111, 'createdby': 'Ad hoc Script - PHARMMD\\Jake.Woods', 'updatedby': None, 'transferstatuscode': 1}, 
                        {'pmdruleid': 10020, 'createdby': 'Ad hoc Script - PHARMMD\\Jake.Woods', 'updatedby': None, 'transferstatuscode': 1}]

rulenames = [{'pmdruleid': 112, 'createdby': 'Ad hoc Script - PHARMMD\\Jake.Woods', 'updatedby': None, 'transferstatuscode': 1}, 
             {'pmdruleid': 10160, 'createdby': 'Ad hoc Script - PHARMMD\\Jake.Woods', 'updatedby': None, 'transferstatuscode': 1}]

if len(activeclientrulelist) > 0:
  for rule in rulenames:
     matching_active_client_rule_items = [y for y in activeclientrulelist if y['pmdruleid'] == rule['pmdruleid']

Expected output

[{'pmdruleid': 112, 'createdby': 'Ad hoc Script - PHARMMD\\Jake.Woods', 'updatedby': None, 'transferstatuscode': 1}, 
 {'pmdruleid': 10160, 'createdby': 'Ad hoc Script - PHARMMD\\Jake.Woods', 'updatedby': None, 'transferstatuscode': 1}]
User123
  • 793
  • 4
  • 10
  • 28

4 Answers4

1

If you want to find the intersection between the 2 lists of dictionaries you could do:

res = [x for x in activeclientrulelist if x in rulenames]

And res will be:

[{'pmdruleid': 112, 'createdby': 'Ad hoc Script - PHARMMD\\Jake.Woods', 'updatedby': None, 'transferstatuscode': 1}, {'pmdruleid': 10160, 'createdby': 'Ad hoc Script - PHARMMD\\Jake.Woods', 'updatedby': None, 'transferstatuscode': 1}]

And if you only want to consider pmdruleid key you can do, no need to loop the rulenames outside the list-comprhension other wise you overwrite the results:

[x for x in activeclientrulelist for y in rulenames if x['pmdruleid'] == y['pmdruleid']]
David
  • 8,113
  • 2
  • 17
  • 36
0

It seems the problem is that the list gets refreshed everytime,so I changed it as follows:

matching_active_client_rule_items=[]
if len(activeclientrulelist) > 0:
  for rule in rulenames:
     matching_active_client_rule_items.extend( [y for y in activeclientrulelist if y['pmdruleid'] == rule['pmdruleid']])



print(matching_active_client_rule_items)

I defined an empty list above and extended the list by the correct item.

Toby Maguire
  • 146
  • 1
  • 11
0

You have couple of problems with your current code:

  1. You have a missing ] at the end in line matching_active_client_rule_items = [y for y in activeclientrulelist if y['pmdruleid'] == rule['pmdruleid'].
  2. With this line: matching_active_client_rule_items = [y for y in activeclientrulelist if y['pmdruleid'] == rule['pmdruleid'], you are overriding a the list object with only the output of the last item in rulenames.

You need to create a list and extend that to fix the code:

matching_active_client_rule_items = []
if len(activeclientrulelist) > 0:
  for rule in rulenames:
     matching_active_client_rule_items.extend([y for y in activeclientrulelist if y['pmdruleid'] == rule['pmdruleid']])

Although this works, you should be using the list comprehension approach as below to get the intersection of the two lists:

matching_active_client_rule_items = [x for x in activeclientrulelist for y in rulenames if x['pmdruleid'] == y['pmdruleid']]
Krishna Chaurasia
  • 8,924
  • 6
  • 22
  • 35
0

Try the following:

activeclientrulelist = [{'pmdruleid': 112, 'createdby': 'Ad hoc Script - PHARMMD\\Jake.Woods', 'updatedby': None, 'transferstatuscode': 1}, 
                        {'pmdruleid': 10160, 'createdby': 'Ad hoc Script - PHARMMD\\Jake.Woods', 'updatedby': None, 'transferstatuscode': 1}, 
                        {'pmdruleid': 10016, 'createdby': 'Ad hoc Script - PHARMMD\\Jake.Woods', 'updatedby': None, 'transferstatuscode': 1}, 
                        {'pmdruleid':111, 'createdby': 'Ad hoc Script - PHARMMD\\Jake.Woods', 'updatedby': None, 'transferstatuscode': 1}, 
                        {'pmdruleid': 111, 'createdby': 'Ad hoc Script - PHARMMD\\Jake.Woods', 'updatedby': None, 'transferstatuscode': 1}, 
                        {'pmdruleid': 10020, 'createdby': 'Ad hoc Script - PHARMMD\\Jake.Woods', 'updatedby': None, 'transferstatuscode': 1}]

rulenames = [{'pmdruleid': 112, 'createdby': 'Ad hoc Script - PHARMMD\\Jake.Woods', 'updatedby': None, 'transferstatuscode': 1}, 
             {'pmdruleid': 10160, 'createdby': 'Ad hoc Script - PHARMMD\\Jake.Woods', 'updatedby': None, 'transferstatuscode': 1},
             {'pmdruleid': 101, 'createdby': 'Ad hoc Script - PHARMMD\\Jake.Woods', 'updatedby': None, 'transferstatuscode': 1}]

result = [check for check in rulenames for rule in activeclientrulelist if rule['pmdruleid']==check['pmdruleid']]
print(result)

In this case, the rulenames: 'pmdruleid': 101 does not belong to the dictionaries in activeclientrulelist, hence omitted!

anurag
  • 1,715
  • 1
  • 8
  • 28