daquezada
daquezada

Reputation: 1037

Inheriting missing keys in a dictionary (Python)

I am creating a Python3 dictionary:

parents = {
    'parent_0': {
        'f_name': 'john',
        'l_name': 'doe',
        'location': 'New York, NY',
        'birth_month': 'september',
        'nationality': 'france',
        },
    'parent_1': {
        'f_name': 'jane',
        'l_name': '',
        'location': '',
        'birth_month': 'may',
        'nationality': ''
    },

}

How can parent_1 inherit the key values for l_name, location and nationality from parent_0?

Upvotes: 0

Views: 102

Answers (2)

Max Power
Max Power

Reputation: 8996

for key in parents['parent_1'].keys:
    if parents['parent_1'] == '':
        parents['parent_1'][key] = parents['parent_0'][key]

Upvotes: 1

Nathan Werth
Nathan Werth

Reputation: 5273

for key, value in parents['parent_1'].items():
    if not value:
        parents['parent_1'][key] = parents['parent_0'][key]

Upvotes: 2

Related Questions