Break out of nested loops with max. 4 lines of code, no matter how deeply they are nested
The problem: breaking out of nested loops
pip install break-out-nested
# Break out of nested loops - a pain in the ***
# How it is usually done:
done = False
for i in range(1, 6, 1): # 1st loop
print('i:', i)
for j in range(1, 11, 2): # 2nd loop
print(' i, j:', i, j)
for k in range(1, 21, 4): # 3rd loop
print(' i,j,k:', i, j, k)
if i % 3 == 0 and j % 3 == 0 and k % 3 == 0:
done = True
break # breaking from 3rd loop
if done: break # breaking from 2nd loop
if done: break # breaking from 1st loop
The solution
# Way easier
from break_out_nested import it, bol
# you need to create new variables as attributes of it,
# because break_out_nested has only access to these variables
it.i, it.j, it.k = 1, 1, 1
# the break condition
def cond(): return it.i % 3 == 0 and it.j % 3 == 0 and it.k % 3 == 0
# The condition will be checked in each loop
# The function that checks the condition has to be passed as the last argument.
# You can pass as many iterables as you want to the function
for it.i, it.j, it.k in bol(range(1, 6, 1), range(1, 11, 2), range(1, 21, 4), cond):
print(it.i, it.j, it.k)
More examples
# More examples
def cond(): return it.i + it.j + it.k == 777
it.i, it.j, it.k = 0, 0, 0
for it.i, it.j, it.k in bol(range(100), range(1000), range(10000), cond):
print(it.i, it.j, it.k)
def cond(): return it.i + it.j + it.k >= 100000
it.i, it.j, it.k = 0, 0, 0
# you don't have to use it.i, it.j, it.k as the loop variables, you can
# use anything you want, but you have to update the variables somewhere
for i, j, k in bol(range(100), range(1000), range(10000), cond):
it.i, it.j, it.k = i * 10, j * 100, k * 100
print(it.i, it.j, it.k)
Metadata
Release files for break-out-nested 0.10
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| break_out_nested-0.10.tar.gz | 3.7 kB | Without provenance | Details for break_out_nested-0.10.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| break_out_nested-0.10-py3-none-any.whl | Python 3 | none | any | Details for break_out_nested-0.10-py3-none-any.whl |
Total release size: 9.1 kB
Release files / break_out_nested-0.10.tar.gz
| Download URL | break_out_nested-0.10.tar.gz |
|---|---|
| Size | 3.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5da8cd628c2325f20e84b57df8f1913312d05fd748c4b933e69bae4849e91bf1
|
|
BLAKE2b-256 checksum How to use checksums |
fb689367b189244380d51c260033e94143bc0b9344cdb862c7c2fdb1ece6903d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.9.13
|
Release files / break_out_nested-0.10-py3-none-any.whl
| Download URL | break_out_nested-0.10-py3-none-any.whl |
|---|---|
| Size | 5.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f792522a23c37c465c7adf4228178e4c866f427c65fd26868bc4f891862af873
|
|
BLAKE2b-256 checksum How to use checksums |
d60464747c9ec50e53eedcdeef5426499de920ed5afcdb6986a862e1d1767fff
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.9.13
|
Security for break-out-nested 0.10
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.