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

Python : more efficient way to do for this

I’m a beginner in python and i’m wondering if there is a classy way to do this :

if societe_var == 'apple':
    bu_list = bu_list_apple
elif societe_var == 'banana':
    bu_list = bu_list_banana
elif societe_var == 'pear' :
    bu_list = bu_list_pear
else :
    bu_list = bu_list_cherry

Best regards,

Kair0

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

>Solution :

Use a dictionary:

bu_list_map = {
    'apple': bu_list_apple,
    'banana': bu_list_banana,
    'pear': bu_list_pear
}

bu_list = bu_list_map.get(societe_var, default=bu_list_cherry)

Use the default argument to the .get() method to handle the fallback case

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