Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Elegant way to return multiple values in a while loop

Tags:

python

loops

I'm pretty much just doing this...

while not something():
    pass

But I want the function something() to return another value other than True or False, specifically a list. In C I'd just return it by plugging in a reference as a parameter but Python doesn't seem to like doing things that way. Therefore in the something() function I would obviously just return 2 values. I'm ok with that. The issue I have is that the code with the while loop doesn't look elegant and so I presume there must be a better way.

In C:

int a[2];
while(!something(a)); //presume that 'a' updates and the loop breaks

In Python:

bool_value, a = something()
while not bool_value:
    bool_value, a = something()

I know it's not such a big deal but just the fact that I have to write the same line twice seems a bit bad. Is there some sort of syntax I can use to call the function and return the 'a' all within the while line?

like image 445
Cameron Avatar asked Dec 28 '25 23:12

Cameron


1 Answers

while True:
    bool_value, a = something()
    if not bool_value:
       break
like image 51
Mangu Singh Rajpurohit Avatar answered Dec 31 '25 00:12

Mangu Singh Rajpurohit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!