Updating python dictionary

andyhume at gmail.com andyhume at gmail.com
Sun Sep 7 17:51:32 EDT 2008


Hello...

I have a dict of key/values and I want to change the keys in it, based
on another mapping dictionary. An example follows:

MAPPING_DICT = {
    'a': 'A',
    'b': 'B',
}

my_dict = {
    'a': '1',
    'b': '2'
}

I want the finished my_dict to look like:

my_dict = {
    'A': '1',
    'B': '2'
}

Whereby the keys in the original my_dict have been swapped out for the
keys mapped in MAPPING_DICT.

Is there a clever way to do this, or should I loop through both,
essentially creating a brand new dict?

Cheers,
Andy.



More information about the Python-list mailing list