From the code posted, i assume messages is a list of dicts.
key_list = [data[key1], data[key2]]
for msg in messages:
if msg['name'] in key_kist:
msg['Flag'] = 2
solved How to change only the last repeated value from a list of dictionaries?