Can you use a while loop on a dictionary in python?

If a value for one of the keys in my dictionary does satisfies a condition, I want to break out of the loop and set a property to True.

what I’m doing so far is:

fooBar = False
for key, value in my_dict.items():
    if (condition):
        fooBar = True

Do I need to use a for loop and iterate through all items in the dictionary, or can I use a while loop?

Answer

You don’t have to continue iterating over the entire dictionary – you could just break out of the loop:

fooBar = False
for key, value in my_dict.items():
    if (condition):
        fooBar = True
        break # Here!