Your end criteria must be formulated a little differently: run the loop while there are items and the bucket is positive. or is not the right operation here.
while unpaid_sales and bucket > 0:
unpaid_sale = unpaid_sales.pop(0)
#do stuff
Answer from tynn on Stack OverflowYour end criteria must be formulated a little differently: run the loop while there are items and the bucket is positive. or is not the right operation here.
while unpaid_sales and bucket > 0:
unpaid_sale = unpaid_sales.pop(0)
#do stuff
Do not use separate whileloops. Do as follows :
while unpaid_sales and bucket > 0 :
unpaid_sale = unpaid_sales.pop(0)
...do stuff
python - Phython - While loop for a list until it is empty - Stack Overflow
Python while loop not ending when list empty - Stack Overflow
Loop until list is not empty in Python - Stack Overflow
While Not Loop for empty list in python - Stack Overflow
images = [ ] for filename in filenames:
Because otherwise without it, if you either refer to it in the loop, it won't exist so you'll get an error. Or if you define it in the loop, you'll redefine it every iteration, destroying all the progress added to it from every previous iteration.
Why do you need to hold an empty cup before pouring water into it?
Based on the other answers, I think the cleanest solutions are
#Handles None return from get_list
for item in get_list() or []:
pass #do something
or the comprehension equiv
result = [item*item for item in get_list() or []]
Use a list comprehension:
def do_something(x):
return x**2
things = []
result = [do_something(x) for x in things]
print result # []
things = [1, 2, 3]
result = [do_something(x) for x in things]
print result # [1, 4, 9]
Loops and conditionals implicitly use bool on all their conditions. The procedure is documented explicitly in the "Truth Value Testing" section of the docs. For a sequence like a list, this usually ends up being a check of the __len__ method.
bool works like this: first it tries the __bool__ method. If __bool__ is not implemented, it checks if __len__ is nonzero, and if that isn't possible, just returns True.
As with all magic method lookup, Python will only look at the class, never the instance (see Special method lookup). If your question is about how to change the behavior, you will need to subclass. Assigning a single replacement method to an instance dictionary won't work at all.
Great question! It's inspecting bool(a), which (usually) calls type(a).__bool__(a).
Python implements certain things using "magic methods". Basically, if you've got a data type defined like so:
class MyExampleDataType:
def __init__(self, val):
self.val = val
def __bool__(self):
return self.val > 20
Then this code will do what it looks like it'll do:
a = MyExampleDataType(5)
b = MyExampleDataType(30)
if a:
print("Won't print; 5 < 20")
if b:
print("Will print; 30 > 20")
For more information, see the Python Documentation: 3.3 Special Method Names.
Hello,
So I was copying a program that removed names from a list and added them to an empty list. Part way through I started experimenting with a print call in a while/for loop, (at this point, I’m just removing names from a list and then printing it.) and I’m kind of confused as to what’s happening. This is the code:
confirmed_user = []
unconfirmed_user = ['bill', 'bob', 'nancy',]
while unconfirmed_user:
for u in unconfirmed_user:
unconfirmed_user.pop()
print(unconfirmed_user)I understand what prints when I have the print call in the for loop, and when I have it outside of both while/for loops. But when I put the print call in the while loop (as shown) I get unexpected results.
I initially expected to just get [ ] back, but I got back:
[‘bill’] [ ]
This is throwing me off. As I expected just to get [ ], and also got [‘bill’] - I’m wondering why it didn’t also print [‘bob’].
I guess I don’t fully understand the sequences of while/for loops. Thanks in advance for any insight.