Reputation: 31
Is it possible to take the value from a key value pair of a dictionary, and make that the key to the entire dictionary?
In case if what I'm asking is unclear,
I have a list of dictionaries, where the dictionaries look like this:
[{'HOUSE NAME': '1A', 'White Bread loaf large': 1, 'Brown Bread loaf large': 1,
'Skimmed Milk Pint': 1, 'Cheddar Cheese Medium 300g': 1}, ...]
and this is what I want to change it to:
[1A : {'White Bread loaf large': 1, 'Brown Bread loaf large': 1,
'Skimmed Milk Pint': 1, 'Cheddar Cheese Medium 300g': 1}, ...]
I'm new to python, and I haven't made an attempt because I really can't think where to start, and I can't seem to find anything online.
Upvotes: 2
Views: 72
Reputation: 42129
For each dictionary in the list create one by extracting the house name. You can do this in a list comprehension:
A = [{'HOUSE NAME': '1A', 'White Bread loaf large': 1,
'Brown Bread loaf large': 1, 'Skimmed Milk Pint': 1,
'Cheddar Cheese Medium 300g': 1}]
B = { d.pop('HOUSE NAME'):d for d in map(dict,A) }
print(B)
{'1A': {'White Bread loaf large': 1, 'Brown Bread loaf large': 1,
'Skimmed Milk Pint': 1, 'Cheddar Cheese Medium 300g': 1}}
Note1: the map(dict,A)
part is there to avoid modifying the dictionaries in A. You don't need it if you're going to discard A afterward or assign the new list back to the A variable
Note2: this assumes that the value of each 'HOUSE NAME' is present in only one dictionary of the list. To merge duplicate values would require a different approach
Upvotes: 0
Reputation: 617
Like this:
old_dict = {'HOUSE NAME': '1A', 'White Bread loaf large': 1, 'Brown Bread loaf large': 1, 'Skimmed Milk Pint': 1, 'Cheddar Cheese Medium 300g': 1}
new_dict = {old_dict.pop('HOUSE NAME'): old_dict}
If you have a list of such dictionaries, then just place this inside a loop
new_dict = {}
for old_dict in old_list_of_dicts:
new_dict[old_dict.pop('HOUSE NAME')]: old_dict
EDIT: Explanation added
Why does this work? dict.pop(key)
does two things. Firstly, it returns the value of the dictionary attached to key. Secondly, it removes that entry from the dictionary. As both of these are things asked for in this question, it makes sense to use this function. It's also faster than looping over every entry when creating a new dictionary as in some other answers [note: I haven't tested this explicitly)]. It should be noted however that this modifies the existing dictionary. If this is not desirable, you can either copy the previous old dictionaries or use one of the other answers provided
Upvotes: 2
Reputation: 2293
So you want to turn a list of dictionaries into a dictionary of dictionaries.
Here's a first, simple way of doing it, by iterating over the dictionaries in the input list:
result = {}
for d in input_list:
result[d[key]] = {k: v for k, v in d.items() if k != key}
The value of the "HOUSE NAME" key (d[key]) is what you want as the key in the new dictionary of dictionaries.
The expression between brackets {} in the last line is called a list comprehension, you should read it as "give me a dictionary of (k, v) pairs, where k and v are obtained by iterating over the pairs (items) in d, but only if k verifies the condition k != key". It's really just a fancy way of removing the "HOUSE NAME" key from each of those dictionaries, but comprehensions are really useful and well worth the investment of studying them.
In fact, we could do all of this in a single comprehension:
result = {d[key]: {k: v for k, v in d.items() if k != key} for d in input_list}
Upvotes: 1
Reputation: 93
Instead of using a list, you will need a dictionary of dictionaries. (use braces instead of square brackets)
Upvotes: 0