Skip to main content

A function to map the keys in a dictionary

Project description

Map Dictionary Keys

Installation

pip install map-dictionary-keys

Usage

from map_dictionary_keys import map_dictionary_keys

d = {'some_key_to_map': 'value'}

mapped_dictionary = map_dictionary_keys(d, lambda key: key.upper())

In the above example, mapped_dictionary will be mapped according to the function some_mapping_function as follows:

mapped_dictionary = {'SOME_KEY_TO_MAP': 'value'}

This function will work for all levels of nested dictionaries. In the following example, both 'sub_dictionary' and 'key_name' will be converted as per the mapping function passed to map_dictionary_keys.

dictionary = {
    'sub_dictionary': {
        'key_name': 'value'
    }
}

It will also work for nested lists of dictionaries. For example, in the following example, 'key_name' will be mapped according to the mapping function provided to map_dictionary_keys.

dictionary = {
    'list_of_dictionaries': [
        {
            'key_name': 'value'
        }
    ]
}

This function will not currently work for lists nested beyond the first level. For example, in the following example, 'key_name' will not be mapped according to the mapping function provided to map_dictionary_keys.

dictionary = {
    'my_list': [
        [
            {
                'key_name': 'value'
            }
        ]
    ]
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for map-dictionary-keys, version 1.0.0
Filename, size File type Python version Upload date Hashes
Filename, size map-dictionary-keys-1.0.0.tar.gz (2.3 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page