0

I have the python dictionary like this

data_dict = {'col_1': [{'size': 3}, {'classes': 'my-3 text-light text-center bg-primary'}, {'styles': [{'font-size': '14px'}]}]}

Using jmespath library, I tried to achieve the desired result of dictionary like this

{'size':3, 'classes':'my-3 text-light text-center bg-primary', 'styles':[{'font-size': '14px'}]}

According to jmespath documentation MultiSelect, I ended up as this:

jmespath.search('*[].{size:size, class:classes, styles:styles}', data_dict)

As a result, I got unnecessary key/value pair of None value dictionary as so

[{'size': 3, 'class': None, 'styles': None}, {'size': None, 'class': 'my-3 text-light text-center bg-primary', 'styles': None}, {'size': None, 'class': None, 'styles': [{'font-size': '14px'}]}]

My question is how can I remove key/value None in order to get my desired result? Thanks.

2

1 Answer 1

3
NewDictList = []
res = [{'size': 3, 'class': None, 'styles': None}, {'size': None, 'class': 'my-3 text-light text-center bg-primary', 'styles': None}, {'size': None, 'class': None, 'styles': [{'font-size': '14px'}]}]
for dict_values in res:
    result = {key: value for key, value in dict_values.items() if value is not None}
    NewDictList.append(result)
print(NewDictList)
#result is [{'size': 3}, {'class': 'my-3 text-light text-center bg-primary'}, {'styles': [{'font-size': '14px'}]}]

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.