How do I re-map python dict keys

Question:

I am working on a program that (among other things) reads a CSV file in (it gets stored as an array of dicts in the form [{col1:data1a,col2:data2a},{col1:data1b,col2:data2b}] ). For each row, as part of other processing, I need to remap those keys to user entered values, which are provided in another dict so they can be used as parameters in an API call. The mapping array is in the form: {badname1:goodname1, badname2:goodname2,...}.

So I’d like to get from:

{badname1:data1, badname2:data2,...}` to `{goodname1:data1, goodname2:data2,...}

I’d like to use something like zip() (although zip() yields {badname1:badname1,...}).

Seems like there should be an obvious solution that is alluding me.

If the data is in a and the mapping in b:

dict(zip(b,a.itervalues()))

I get close, but it will only work in cases where the fields are known to be in the same order I think.

Asked By: acrosman

||

Answers:

name_map = {'oldcol1': 'newcol1', 'oldcol2': 'newcol2', 'oldcol3': 'newcol3'...}

for row in rows:
    # Each row is a dict of the form: {'oldcol1': '...', 'oldcol2': '...'}
    row = dict((name_map[name], val) for name, val in row.iteritems())
    ...

Or in Python2.7+ with Dict Comprehensions:

for row in rows:
    row = {name_map[name]: val for name, val in row.items()}
Answered By: elo80ka
rows = [{"col1":"data1a","col2":"data2a"},{"col1":"data1b","col2":"data2b"}]
name_map = {"col1":"newcol1","col2":"newcol2"}

new_rows = [dict(zip(map(lambda x: name_map[x], r.keys()), r.values())) for r in rows]

Is this what you are after?

Answered By: Jon

If you are using Python 2.7 or Python 3.x, you can use a dictionary comprehension. This is equivalent elo80ka’s answer (which used a list comprehension), but produces slightly more readable code.

name_map = {'oldcol1': 'newcol1', 'oldcol2': 'newcol2', 'oldcol3': 'newcol3'...}

for row in rows:
    # Each row is a dict of the form: {'oldcol1': '...', 'oldcol2': '...'}
    row = {name_map[name]: val for name, val in row.iteritems()}
    ...
Answered By: Adam Paetznick

This function will let you remap only the keys you need to change.

def key_remapper(dictionary: dict):
    return {remap_dictionary.get(k, v): v for k, v in dictionary.items()}
Answered By: kblst

Python 3.7

d = {'badname1': 'data1', 'badname2': 'data2'}
m = {'badname1': 'goodname1', 'badname2': 'goodname2'}

{ m[k]:d[k] for k in d }

Outputs

{'goodname1': 'data1', 'goodname2': 'data2'}
Answered By: Eskandar

Based on this examples:

edited_dict = {'oldname1': 'data1', 'oldname2': 'data2', 'goodname3': 'data3'}
remaped_key = {'oldname1': 'key_1', 'oldname2': 'key_2'}

{ remaped_key[key]:edited_dict[key] for key in edited_dict if key in remaped_key }
# output
{'key_1': 'data1', 'key_2': 'data2'}

# same result:
dict((remaped_key[key], edited_dict[key]) for key, value in edited_dict.items() if key in remaped_key)
# output
{'key_1': 'data1', 'key_2': 'data2'}

Or remaped only wanted keys:

edited_dict = {'oldname1': 'data1', 'oldname2': 'data2', 'goodname3': 'data3'}
remaped_key = {'oldname1': 'key_1', 'oldname2': 'key_2'}

dict((remaped_key[key], edited_dict[key]) if key in remaped_key else (key, value) for key, value in edited_dict.items())
# output
{'key_1': 'data1', 'key_2': 'data2', 'goodname3': 'data3'}
Answered By: Rosta Kosta
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.