I have a list of dictionaries which is,
ip_list = [{'1403': [-56, -58], 'data': '1'},
{'1403': [-56, -58], 'data': '0'}]
Now I need to add a new key(i.e., "mac") and value(i.e., "xyz") in a dictionary if dictionary contains 'data' = 1 and the outcome should be,
expected_outcome = [{'1403': [-56, -58], 'data': '1', 'mac':'xyz'},
{'1403': [-56, -58], 'data': '0'}]
I have tried with the,
list_dict_comp = [dict(item, **{'mac':'xyz'}) for item in ip_list]
Whereas, the above expression gives,
list_dict_comp = [{'1403': [-56, -58], 'data': '1', 'mac':'xyz'},
{'1403': [-56, -58], 'data': '0', 'mac':'xyz'}]
Can anyone help me out to achieve the "expected_outcome" using both list and dict comprehension together in a single expression?