Reputation: 587
Looking for an efficient way to modify a dictionary
I have a list of dictionaries in this format:
>>> x = [{'server': 's1', 'disk' : 'd1', 'size': 10},{'server': 's2', 'disk' : 'd2', 'size': 20},{'server': 's3', 'disk' : 'd3', 'size': 30}, {'server': 's1', 'disk' : 'd4', 'size': 40}]
I'd like it to be restructured so the value of 'server' becomes the key and contains a list of dictionaries with disk and size information that are all within that server
{'s1': [{'disk': 'd1', 'size': 10}, {'disk': 'd4', 'size': 40}], 's2': [{'disk': 'd2', 'size': 20}], 's3': [{'disk': 'd3', 'size': 30}]}
This is what ive tried:
>>> y = {y[i.pop('server')].append(i) if i['server'] in y else i.pop('server'): [i] for i in x}
>>> y
{'s1': [{'disk': 'd4', 'size': 40}], 's2': [{'disk': 'd2', 'size': 20}], 's3': [{'disk': 'd3', 'size': 30}]}
>>> out = {'s1' : [{'disk' : 'd1', 'size': 10},{'disk' : 'd4', 'size': 40}], 's2' : [{'disk' : 'd2', 'size': 20}], 's3' : [{'disk' : 'd3', 'size': 30}]}
Upvotes: 1
Views: 135
Reputation: 20490
The approach you have is fine, but I might suggest creating a new dictionary instead of modify what we already have For that, you can iterate over the list and create your required dictionary as follows
x = [{'server': 's1', 'disk' : 'd1', 'size': 10},{'server': 's2', 'disk' : 'd2', 'size': 20},{'server': 's3', 'disk' : 'd3', 'size': 30}, {'server': 's1', 'disk' : 'd4', 'size': 40}]
from collections import defaultdict
dct = defaultdict(list)
#Iterate over the list
for item in x:
#Use server value as key, and the remainining k,v pairs as dict and append them to a list
dct[item['server']].append({k:v for k,v in item.items() if k != 'server'})
print(dict(dct))
Or if you want to follow the dict.pop()
approach, pop the key server
and use it as a key in your dictionary
x = [{'server': 's1', 'disk' : 'd1', 'size': 10},{'server': 's2', 'disk' : 'd2', 'size': 20},{'server': 's3', 'disk' : 'd3', 'size': 30}, {'server': 's1', 'disk' : 'd4', 'size': 40}]
from collections import defaultdict
dct = defaultdict(list)
#Iterate over the list
for item in x:
#Get server value after popping server key
server = item.pop('server')
#Use that value and append the remaining dict to a list
dct[server].append(item)
print(dict(dct))
The output will be same in both cases, which is
{'s1': [{'disk': 'd1', 'size': 10}, {'disk': 'd4', 'size': 40}], 's2': [{'disk': 'd2', 'size': 20}], 's3': [{'disk': 'd3', 'size': 30}]}
Upvotes: 1