If we have a dict
we can easily iterate through its values:
a = {'mon': 10, 'tue': 13, 'wed': 6, 'thu': 24, 'fri': 15}
for v in a:
print(v)
I also have a nested loop like this:
while True:
# some conditions to break the outer loop
for v in a:
# some conditions to break the inner loop
print(v)
Every time it runs a cycle in the outer loop, it starts again from the “first” dict
element (I’m aware dictionaries are not ordered, but if they are not modified the cycles should be the same).
Instead, I want it continues from the last key, and if it ends, starts again from the beginning.
Example:
1st outer cycle: 'mon', 'tue' (then breaks then inner cycle)
2nd outer cycle: 'wed' (then breaks then inner cycle)
3rd outer cycle: 'thu', 'fri', 'mon' (then breaks then inner cycle)
4th outer cycle: 'tue', 'wed' (then breaks then inner cycle)
breaks the outer cycle
My attempt was to save the last key and skip the previous ones, but is not straightforward and I have to restart the cycle if it ends before I have iterated through all keys.
Is there a simple way in Python to achieve this?