1

This loop does not work as expected. It prints all 4 elements (expected 3)

guj = ["बळींचाी", "आठ़वले", "मोठय़ा", "धाांची"]

for y in guj:
    for i in y:
        x = i.encode("raw_unicode_escape")
        if x[:5][-3:] not in [b"090", b"091", b"092", b"093", b"094"]:
            break
    print(y)

The third word should not be printed because it contains a character having 095 series that is invalid.

# "य़".encode("raw_unicode_escape") # b'\\u095f'

output.txt

बळींचाी
आठ़वले
मोठय़ा
धाांची

expected.txt

बळींचाी
आठ़वले
धाांची
shantanuo
  • 31,689
  • 78
  • 245
  • 403
  • @TomServo I think it's clear what OP is asking, I don't think we should discriminate because his example data happens to be non-English characters. – augurar Apr 14 '21 at 02:42
  • 1
    @shantanuo Here's what I was going to post as an answer: you can solve your issue by moving the `print()` call into a [`else:` clause on the inner loop](https://docs.python.org/3/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops). That way it will only be executed if the loop did not encounter a `break` statement. If your real code is more complex you can instead set a variable like `found = False` before the inner loop, set `found = True` before the `break`, and add a check `if not found: print(y)` – augurar Apr 14 '21 at 02:45
  • Voting to reopen. Based on the OP's example, it's clear he does not want to break out of both loops, actually he wants to do something like `continue` the outer loop after breaking out of the inner loop. – augurar Apr 14 '21 at 02:48

0 Answers0