Double iteration in for comprehension without 2d list

20 Views Asked by At

I would like to perform a double 'for' loop within a for-comprehension. However, I do not want to do it under the typical conditions, such as:

sentences = ['hello what are you doing?', 'trying to figure this out!']
[c for word in sentences for c in word]

Instead, I would like to perform this double iteration with condition, but in a for-comprehension:

words = ["snake", "porcupine", "lizard"]
substrings = ["sn", "o", "ke"]
new = []
for word in words:
    for substr in substrings:
        if substr in word:
            new.append(word)
new = set(new)
print(new)

Any help is appreciated!

1

There are 1 best solutions below

0
You_Donut On

Just figured it out, nevermind. Simply use any():

new = [word for word in words if any(substr in word for substr in substrings)]