I have the following dictionary
{"contact_uuid": ["67460e74-02e3-11e8-b443-00163e990bdb"], "choices": ["None"], "value": [""], "cardType": [""], "step": ["None"], "optionId": ["None"], "path": [""], "title": [""], "description": [""], "message": [""]}
But I'd like to remove the lists so it looks like this:
{'contact_uuid': '67460e74-02e3-11e8-b443-00163e990bdb', 'choices': "None", "value": [""], 'cardType': [""], 'step': ['None'], 'optionId': ['None'], 'path: [''], 'title': "", 'description': "", 'message': ""}
Is there a simple way to do this. I think I might be able to iterate through and remove the list. Thanks
CodePudding user response:
I didn't realize that parse_qsl had a keep_blank attribute. It still returns the values in a list though which isn't always great especially in my case.
json_url = (parse_qs(url, keep_blank_values=True))
CodePudding user response:
In order to produce the required output you will need a list (set) of keys that should not be modified. Something like this:
RETAIN = {'value', 'cardType', 'step', 'optionId', 'path'}
dict_ = {
"contact_uuid": ["67460e74-02e3-11e8-b443-00163e990bdb"],
"choices": ["None"],
"value": [""],
"cardType": [""],
"step": ["None"],
"optionId": ["None"],
"path": [""],
"title": [""],
"description": [""],
"message": [""]
}
for k, v in dict_.items():
if k not in RETAIN:
if isinstance(v, list):
dict_[k] = v[0]
print(dict_)
Output:
{'contact_uuid': '67460e74-02e3-11e8-b443-00163e990bdb', 'choices': 'None', 'value': [''], 'cardType': [''], 'step': ['None'], 'optionId': ['None'], 'path': [''], 'title': '', 'description': '', 'message': ''}