Skip to main content
Became Hot Network Question
Tweeted twitter.com/StackCodeReview/status/1413558585083240452
Added more to dicts
Source Link

I have a dictionary in Python (uglyDict) that has some really un-cool key names. I would like to rename them based on values from another dictionary (keyMapping). Here's an example of the dictionaries:

uglyDict = {
    "ORDER_NUMBER": "6492",
    "Ship To - Name": "Noah""J.B Brawls",
    "Ship To - Location"Address 1": "my"42 crib"BAN ROAD",
    "Ship To - City": "Jimville",
    "Ship To - State": "VA",
    "Ship To - Postal Code": "42691"
}

keyMapping = {
    "Market - Store Name": "WSCUSTOMERID",
    "Ship To - Name": "ShipToCompany",
    "Ship To - Location"Country": "ShipLocation""ShipToCountryCode",
    "Ship To - Postal Code": "ShipToZipcode",
    "Ship To - City": "ShipToCity",
    "Date - Order Date": "OrderDate",
    "Gift - Message": "SpecialInstructions",
    "Customer Email": "ShipToEmail",
    "Ship To - Address 2": "ShipToAddress2",
    "Ship To - Address 1": "ShipToAddress1",
    "Ship To - Phone": "ShipToPhoneNum",
    "Order - Number": "ORDER_NUMBER",
    "Ship To - State": "ShipToState",
    "Carrier - Service Selected": "ShipMethod",
    "Item - SKU": "PRODUCT_NUMBER",
    "Item - Qty": "Quantity"
}

Here is my code so far to rename uglyDict's keys:

prettyDict = {}
for mkey, mval in keyMapping.items():
    for ukey in uglyDict.keys():
        if mkey == ukey:
            prettyDict[mval] = uglyDict[mkey]

print(prettyDict)

The code works as desired, and prints the dictionary with renamed keys as shown below:

{'ORDER_NUMBER': '6492', 'ShipToCompany': 'Noah''J.B Brawls', 'ShipLocation''ShipToAddress1': 'my'42 crib'BAN ROAD', 'ShipToCity': 'Jimville', 'ShipToState': 'VA', 'ShipToZipcode': '42691'}

My question is, is there any more efficient/more Pythonic way to do this? Preferably one where I don't have to loop over the keys of both dictionaries. I am using this code with much larger dictionaries and performance is needed.
Any insight is welcome!

I have a dictionary in Python (uglyDict) that has some really un-cool key names. I would like to rename them based on values from another dictionary (keyMapping). Here's an example of the dictionaries:

uglyDict = {
    "Ship To - Name": "Noah",
    "Ship To - Location": "my crib"
}

keyMapping = {
    "Ship To - Name": "ShipToCompany",
    "Ship To - Location": "ShipLocation"
}

Here is my code so far to rename uglyDict's keys:

prettyDict = {}
for mkey, mval in keyMapping.items():
    for ukey in uglyDict.keys():
        if mkey == ukey:
            prettyDict[mval] = uglyDict[mkey]

print(prettyDict)

The code works as desired, and prints the dictionary with renamed keys as shown below:

{'ShipToCompany': 'Noah', 'ShipLocation': 'my crib'}

My question is, is there any more efficient/more Pythonic way to do this? Preferably one where I don't have to loop over the keys of both dictionaries. I am using this code with much larger dictionaries and performance is needed.
Any insight is welcome!

I have a dictionary in Python (uglyDict) that has some really un-cool key names. I would like to rename them based on values from another dictionary (keyMapping). Here's an example of the dictionaries:

uglyDict = {
    "ORDER_NUMBER": "6492",
    "Ship To - Name": "J.B Brawls",
    "Ship To - Address 1": "42 BAN ROAD",
    "Ship To - City": "Jimville",
    "Ship To - State": "VA",
    "Ship To - Postal Code": "42691"
}

keyMapping = {
    "Market - Store Name": "WSCUSTOMERID",
    "Ship To - Name": "ShipToCompany",
    "Ship To - Country": "ShipToCountryCode",
    "Ship To - Postal Code": "ShipToZipcode",
    "Ship To - City": "ShipToCity",
    "Date - Order Date": "OrderDate",
    "Gift - Message": "SpecialInstructions",
    "Customer Email": "ShipToEmail",
    "Ship To - Address 2": "ShipToAddress2",
    "Ship To - Address 1": "ShipToAddress1",
    "Ship To - Phone": "ShipToPhoneNum",
    "Order - Number": "ORDER_NUMBER",
    "Ship To - State": "ShipToState",
    "Carrier - Service Selected": "ShipMethod",
    "Item - SKU": "PRODUCT_NUMBER",
    "Item - Qty": "Quantity"
}

Here is my code so far to rename uglyDict's keys:

prettyDict = {}
for mkey, mval in keyMapping.items():
    for ukey in uglyDict.keys():
        if mkey == ukey:
            prettyDict[mval] = uglyDict[mkey]

print(prettyDict)

The code works as desired, and prints the dictionary with renamed keys as shown below:

{'ORDER_NUMBER': '6492', 'ShipToCompany': 'J.B Brawls', 'ShipToAddress1': '42 BAN ROAD', 'ShipToCity': 'Jimville', 'ShipToState': 'VA', 'ShipToZipcode': '42691'}

My question is, is there any more efficient/more Pythonic way to do this? Preferably one where I don't have to loop over the keys of both dictionaries. I am using this code with much larger dictionaries and performance is needed.
Any insight is welcome!

added 2 characters in body
Source Link

I have a dictionary in Python (uglyDict) that has some really un-cool key names. I would like to rename them based on values from another dictionary (keyMapping). Here's an example of the dictionaries:

uglyDict = {
    "Ship To - Name": "Noah",
    "Ship To - Location": "my crib"
}

keyMapping = {
    "Ship To - Name": "ShipToCompany",
    "Ship To - Location": "ShipLocation"
}

Here is my code so far to rename uglyDict's keys:

prettyDict = {}
for mkey, mval in keyMapping.items():
    for ukey in uglyDict.keys():
        if mkey == ukey:
            prettyDict[mval] = uglyDict[mkey]

print(prettyDict)

The code works as desired, and prints the dictionary with renamed keys as shown below:

{'ShipToCompany': 'Noah', 'ShipLocation': 'my crib'}

My question is, is there any more efficient/more Pythonic way to do this? Preferably one where I don't have to loop over the keys of each dictionaryboth dictionaries. I am using this code with much larger dictionaries and performance is needed.
Any insight is welcome!

I have a dictionary in Python (uglyDict) that has some really un-cool key names. I would like to rename them based on values from another dictionary (keyMapping). Here's an example of the dictionaries:

uglyDict = {
    "Ship To - Name": "Noah",
    "Ship To - Location": "my crib"
}

keyMapping = {
    "Ship To - Name": "ShipToCompany",
    "Ship To - Location": "ShipLocation"
}

Here is my code so far to rename uglyDict's keys:

prettyDict = {}
for mkey, mval in keyMapping.items():
    for ukey in uglyDict.keys():
        if mkey == ukey:
            prettyDict[mval] = uglyDict[mkey]

print(prettyDict)

The code works as desired, and prints the dictionary with renamed keys as shown below:

{'ShipToCompany': 'Noah', 'ShipLocation': 'my crib'}

My question is, is there any more efficient/more Pythonic way to do this? Preferably one where I don't have to loop over the keys of each dictionary. I am using this code with much larger dictionaries and performance is needed.
Any insight is welcome!

I have a dictionary in Python (uglyDict) that has some really un-cool key names. I would like to rename them based on values from another dictionary (keyMapping). Here's an example of the dictionaries:

uglyDict = {
    "Ship To - Name": "Noah",
    "Ship To - Location": "my crib"
}

keyMapping = {
    "Ship To - Name": "ShipToCompany",
    "Ship To - Location": "ShipLocation"
}

Here is my code so far to rename uglyDict's keys:

prettyDict = {}
for mkey, mval in keyMapping.items():
    for ukey in uglyDict.keys():
        if mkey == ukey:
            prettyDict[mval] = uglyDict[mkey]

print(prettyDict)

The code works as desired, and prints the dictionary with renamed keys as shown below:

{'ShipToCompany': 'Noah', 'ShipLocation': 'my crib'}

My question is, is there any more efficient/more Pythonic way to do this? Preferably one where I don't have to loop over the keys of both dictionaries. I am using this code with much larger dictionaries and performance is needed.
Any insight is welcome!

Source Link

Efficient renaming of dict keys from another dict's values - Python

I have a dictionary in Python (uglyDict) that has some really un-cool key names. I would like to rename them based on values from another dictionary (keyMapping). Here's an example of the dictionaries:

uglyDict = {
    "Ship To - Name": "Noah",
    "Ship To - Location": "my crib"
}

keyMapping = {
    "Ship To - Name": "ShipToCompany",
    "Ship To - Location": "ShipLocation"
}

Here is my code so far to rename uglyDict's keys:

prettyDict = {}
for mkey, mval in keyMapping.items():
    for ukey in uglyDict.keys():
        if mkey == ukey:
            prettyDict[mval] = uglyDict[mkey]

print(prettyDict)

The code works as desired, and prints the dictionary with renamed keys as shown below:

{'ShipToCompany': 'Noah', 'ShipLocation': 'my crib'}

My question is, is there any more efficient/more Pythonic way to do this? Preferably one where I don't have to loop over the keys of each dictionary. I am using this code with much larger dictionaries and performance is needed.
Any insight is welcome!