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

Map elements from list with items from dictionary

I have a list of "important" checkpoints which are passed by a user:

important_checkpoints = ['checkpoint_1', 'checkpoint_2', 'checkpoint_3', 
                         'checkpoint_4', 'checkpoint_5']

The term "important" means, that from all possible checkpoints (‘checkpoint_1’, … ,checkpoint_99) the user can pass, I am only interested in the checkpoints listed in important_checkpoints.

Additionally I have a dictionary containing the checkpoints which have actually have been used by the users:

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

user_checkpoints = {"id_1": "checkpoint_11", "id_2": "checkpoint_3", "id_3": "checkpoint_4", 
                    "id_4": "checkpoint_45", "id_5": "checkpoint_68", "id_6": "checkpoint_1", 
                   "id_7": "checkpoint_1"}

Solution to example:
Now I want to get a dictionary which tells me which user-ID passed one of the "important" checkpoints:

{"checkpoint_1": [id_6, id_7], "checkpoint_2": [], "checkpoint_3": [id_2], "checkpoint_4": [id_3], 
"checkpoint_5": []}

My not finished solution
I was able to find passed important checkpoints (here: ['checkpoint_1', 'checkpoint_3', 'checkpoint_4']) but I was not able to match the corresponding IDs.

activated_checkpoints_by_user = [] # activated_forms_for_user_concerns
for key in important_checkpoints:
    if key in list(user_checkpoints.values()):
        activated_checkpoints_by_user.append(key) 

>> activated_checkpoints_by_user
>> ['checkpoint_1', 'checkpoint_3', 'checkpoint_4']

>Solution :

You basically want to map the values in the dictionary user_checkpoints back to the keys.
In Python there is no direct way to do it but you can itemize the dictionary and use a loop to get the result you want:

result_dict ={}
for imp_checkpoint in important_checkpoints:
  result_dict[imp_checkpoint]=[user_id for user_id, checkpoint in user_checkpoints.items() if checkpoint == imp_checkpoint]
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