Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to update the string with priority order in the dictionary

I have list of dict below

data = 
[ { 'Pencil': 'Green' }, { 'Pen': 'N/A' }, { 'Scale': 'Red' }, { 'Compass': 'N/A'}]

My priority order is below

priority_order = {'Red':4, 'Orange':3, 'Yellow':2, 'Green':1, 'Undefined': 0}

I have main variable which has to update with in priority order of values in data list of dictionary, by default main is

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

main = 'Undefined'

Code is below

for each in data:
    for k,v in each.items():
        if priority_order[v] > priority_order[main]:
            main = priority_order[v]

I am getting key error for this

My expected out is ‘Red’ as scale is having ‘Red’

>Solution :

data =[ { 'Pencil': 'Green' }, { 'Pen': 'N/A' }, { 'Scale': 'Red' }, { 'Compass': 'N/A'}]
priority_order = {'Red':4, 'Orange':3, 'Yellow':2, 'Green':1, 'N/A': 0}
main = 'N/A'

for each in data:
    for k,v in each.items():
        if priority_order[v] > priority_order[main]:
            main = v
print(main)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading