Logo

Programming-Idioms

History of Idiom 42 > diff from v23 to v24

Edit summary for version 24 by :
New Python implementation by user [cym13]

Version 23

2015-11-30, 12:37:28

Version 24

2016-01-26, 07:13:11

Idiom #42 Continue outer loop

Print each item v of list a which in not contained in list b.
For this, write an outer loop to iterate on a and an inner loop to iterate on b.

Idiom #42 Continue outer loop

Print each item v of list a which in not contained in list b.
For this, write an outer loop to iterate on a and an inner loop to iterate on b.

Code
for v in a:
    try:
        for u in b:
            if v == u:
                raise Exception()
        print(v)
    except Exception:
        continue
Comments bubble
Note that using two loops like this in python is by itself very un-idiomatic. Also one would be wise to define a custom exception to avoid hiding "real" exceptions.