I would like to know what is the most elegant or pythonic way to copy specific values from one dictionary into another, only if the values are not None
, empty, or empty dict.
The new dictionary will have different key names than the original one.
For example, let's assume I got a response from API and I converted json
to dict
customer = [{
'name': 'John',
'email': 'johnsmith@gmail.com',
'phoneNumber': '9999999',
'country': 'USA',
'city': None,
'preferences': {}
}]
new_customer_dict = {}
for client in customer:
if client.get('name'):
new_customer_dict ['customer_name'] = client['name']
if client.get('email'):
new_customer_dict['customer_email'] = client['email']