Pythonic way to combine for-loop and if-statement

You can use generator expressions like this:

gen = (x for x in xyz if x not in a)

for x in gen:
    print(x)

Leave a Comment