Search code examples
pythondictionarydata-structuresgeojson

Shifting values from one key to another key in python dictionary


I've the below dictionary (Geojson):

'properties': {
            'fill': '#ffffff', 'fill-opacity': 1, 'stroke': '#ffffff',
'stroke-opacity': 1, 'stroke-width': 1.5, 'title': '0.00 m',
'time': '2000-01-31'
    }

What would be the easiest way to make it into as below, by moving certain values to new keys within properties.

'properties': {
        'style': {
            'fill': '#ffffff', 'fill-opacity': 1, 'stroke': '#ffffff',
'stroke-opacity': 1, 'stroke-width': 1.5, 'title': '0.00 m'
        },
        'time': '2000-01-31'
    }
}

Any feedback would be helpful. Thanks.


Solution

  • You could pop the time and build a new dict like this:

    properties = {
                'fill': '#ffffff', 'fill-opacity': 1, 'stroke': '#ffffff',
    'stroke-opacity': 1, 'stroke-width': 1.5, 'title': '0.00 m',
    'time': '2000-01-31'
        }
    
    time = properties.pop('time')
    new_properties = {'style': properties, 'time':time}
    
    print(new_properties)
    # {'style': {'fill': '#ffffff', 'fill-opacity': 1, 'stroke': '#ffffff', 
    #            'stroke-opacity': 1, 'stroke-width': 1.5, 'title': '0.00 m'},
    #   'time': '2000-01-31'}