Reputation: 297
Good morning. I have some nested dictionaries with multiple items per key. I need to extract only the pairs that include items that only have 'ans' and 'val' pairs, if the item includes 'ans', 'type','value' etc then i want to remove it. an example of the dictionary i have and the expected output are below. Any advice is welcome, thanks so much
data
dict_i_have = {
"x19": [
{
"ans": "Enter number",
"type": "number",
"value": "input_val",
"validators": [
{"dype": "int", "invalid": "Enter number between 0 and 100", "min": 0, "max": 100},
{"vtype": "no", "message": "Please enter a value"},
],
},
{"ans": "One year or less", "val": "-10"},
{"ans": "Do not know", "val": "1"},
{"ans": "Other", "val": "3"},
],
"x20": [
{
"ans": "Enter number",
"type": "number",
"value": "input_val",
"validators": [
{"dype": "int", "invalid": "Enter number between 0 and 50", "min": 0, "max": 50},
{"vtype": "no", "message": "Please enter a value"},
],
},
{"ans": "Six months or less", "val": "10"},
],
}
expected output
dict_i_want = {'x19': [{'ans': 'One year or less', 'val': '-10'},
{'ans': 'Do not know', 'val': '1'},
{'ans': 'Other', 'val': '3'}],
'x20': [{'ans': 'Six months or less', 'val': '10'}]}
Upvotes: 1
Views: 67
Reputation: 151
Try the below simple solution for the problem,
dict_i_have = {
"x19": [
{
"ans": "Enter number",
"type": "number",
"value": "input_val",
"validators": [
{"dype": "int", "invalid": "Enter number between 0 and 100", "min": 0, "max": 100},
{"vtype": "no", "message": "Please enter a value"},
],
},
{"ans": "One year or less", "val": "-10"},
{"ans": "Do not know", "val": "1"},
{"ans": "Other", "val": "3"},
],
"x20": [
{
"ans": "Enter number",
"type": "number",
"value": "input_val",
"validators": [
{"dype": "int", "invalid": "Enter number between 0 and 50", "min": 0, "max": 50},
{"vtype": "no", "message": "Please enter a value"},
],
},
{"ans": "Six months or less", "val": "10"},
],
}
# Actual Solution starts here
for key1 in dict_i_have:
for val in dict_i_have[key1]:
if len(val) > 2 :
dict_i_have[key1].remove(val)
print(dict_i_have)
Upvotes: 1
Reputation: 46
Using dict keys
function can get the keys in the dict.
Then compare it with {"ans", "val"}
to filter out the unwanted dict.
for key in dict_i_have.keys():
dict_i_have[key] = [item for item in dict_i_have[key] if set(item.keys()) == {'ans', 'val'}]
Upvotes: 0
Reputation: 169268
You can do this with a dictionary comprehension:
dict_i_have = {
# elided for brevity
}
dict_i_want = {
key: [
{"ans": q["ans"], "val": q["val"]} for q in questions if "val" in q
]
for (key, questions) in dict_i_have.items()
}
print(dict_i_want)
Or if you want to keep references to the original dicts instead of copying them into new ones,
dict_i_want = {
key: [q for q in questions if set(q) == {"ans", "val"}]
for (key, questions) in dict_i_have.items()
}
print(dict_i_want)
Upvotes: 0
Reputation: 9797
Quite literally filter your list of dicts to those that have exactly two keys, with both key values you're interested in.
dict_i_want = dict()
for key, values in dict_i_have.items():
subdicts = [d for d in values if len(d) == 2 and 'ans' in d and 'val' in d]
dict_i_want[key] = subdicts
Upvotes: 1