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: execute on last iteration of while loop

My goal is to execute an operation on the last operation of while loop only. My while loop is nested in a for loop.

Currently I go for:

for i in range(10):
    indicator = False
    while(...):
       do something
       indicator = True  
    if indicator == True:
        do one operation
        indicator = False
    do something not in while loop

While it seems to work, it doesn’t look elegant. Is there a more pythonic way?

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 :

If you are not using break in while loop, you can use while ... else expression.

https://www.pythontutorial.net/python-basics/python-while-else/

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