Reputation: 405
I have a list of dictionaries with information I'd like to replace under the keys of each dictionary.
So, I'm thinking of iterating through each dictionary and replacing the values.
I have another dictionary with the values I'd like to replace as keys and the values which should be the final ones as values.
It goes like this:
listOfDicts = [{'value_1' : 'A', 'value-2' : 'B', 'valu&_3' : 'C'}, {'value-1' : 'D', 'value_2' : 'E', 'value-3' : 'F'}, {'value_1' : 'G', 'value_2' : 'H', 'value_3' : 'I'}]
Then I have another dictionary to use as basis for fixing this information:
fixer = {'value-2' : 'value_2', 'value&_3' : 'value_3', 'value-3' : 'value_3', ...}
How could I make this replacement?
- Edit:
The desired output would be somethink like this:
listOfDicts = [{
'value_1' : 'A',
'value_2' : 'B',
'value_3' : 'C'},
{'value_1' : 'D',
'value_2' : 'E',
'value_3' : 'F'},
...}
Upvotes: 2
Views: 6650
Reputation: 9863
If I've understood correctly you want something like this?
import json
import copy
listOfDicts = [
{
"valu&_3": "C",
"value-2": "B",
"value_1": "A"
},
{
"value-1": "D",
"value-3": "F",
"value_2": "E"
},
{
"value_1": "G",
"value_2": "H",
"value_3": "I"
}
]
fixer = {
"valu&_3": "value_3",
"value-2": "value_2",
"value-3": "value_3"
}
newDict = copy.deepcopy(listOfDicts)
for oldDct in newDict:
for k2, v2 in fixer.items():
value = oldDct.pop(k2, None)
if value:
oldDct[v2] = value
print('listOfDicts'.center(80, '-'))
print(json.dumps(listOfDicts, indent=4))
print('newDict'.center(80, '-'))
print(json.dumps(newDict, indent=4))
Output:
----------------------------------listOfDicts-----------------------------------
[
{
"valu&_3": "C",
"value-2": "B",
"value_1": "A"
},
{
"value-1": "D",
"value-3": "F",
"value_2": "E"
},
{
"value_1": "G",
"value_2": "H",
"value_3": "I"
}
]
------------------------------------newDict-------------------------------------
[
{
"value_1": "A",
"value_3": "C",
"value_2": "B"
},
{
"value-1": "D",
"value_2": "E",
"value_3": "F"
},
{
"value_1": "G",
"value_2": "H",
"value_3": "I"
}
]
Upvotes: 2
Reputation: 1118
This should work for you:
listOfDicts = [{'value_1' : 'A', 'value-2' : 'B', 'value&_3' : 'C'}, {'value-1' : 'D', 'value_2' : 'E', 'value-3' : 'F'}, {'value_1' : 'G', 'value_2' : 'H', 'value_3' : 'I'}]
fixer = {'value-1' : 'value_1', 'value-2' : 'value_2', 'value&_3' : 'value_3', 'value-3' : 'value_3'}
for i in range(len(listOfDicts)):
keys = list(listOfDicts[i].keys())
temp_dict = dict()
for item in keys:
if item in fixer:
temp_dict[fixer[item]] = listOfDicts[i][item]
else:
temp_dict[item] = listOfDicts[i][item]
listOfDicts[i] = temp_dict
print(listOfDicts)
Output
[{'value_1': 'A', 'value_2': 'B', 'value_3': 'C'}, {'value_1': 'D', 'value_2': 'E', 'value_3': 'F'}, {'value_1': 'G', 'value_2': 'H', 'value_3': 'I'}]
What this program does is that it iterates through each item in the list and gets the keys in the dictionary in the current item. It then creates a temporary dictionary (temp_dict
), which will later store the fixed values in it. Then, the program goes through and sees whether or not any keys need to be fixed, and if so, it fixes the keys based on the fixer
dictionary. Finally, it replaces the item being iterated in listOfDicts
with the fixed values.
Upvotes: 1
Reputation: 476534
We can fix every dictionary with a function:
fixer = {'value-2' : 'value_2', 'value&_3' : 'value_3', 'value-3' : 'value_3'}
def fix_dictionary(dict_):
return { fixer.get(k, k): v for k, v in dict_.items() }
This will for a given dict_
construct a new dictionary where keys that are in the fixer
will be "fixed" (replaced by the corresponding value in the fixer
dictionary).
We can then generate a new list of fixed dictionaries with:
[fix_dictionary(dict_) for dict_ in listOfDicts]
Or we can eliminate the function, and use a one liner:
[{ fixer.get(k, k): v for k, v in dict_.items() } for dict_ in listOfDicts]
Upvotes: 4