I recently learnt that we can include multiple loops in a python list comprehension, just like the sequence of the for loop in traditional manner.
So, a nested for loop instead another for loop can be added as a list comprehension as under:
result_list = [sub for part in parts for sub in part if sub > 10]
However, I got confused in one scenario where the number of 'sub' is too long, while the condition of if can be completely ignored on the rest if even one sub breaks the rule. So instead of checking all the sub in the part, I break the loop when the condition breaks and move on to next parts.
I am sharing a scenario from the code I am working on.
for part in parts:
for sub in part:
if sub not in a_list:
var = False
break
if var == True:
b_list.append(part)
The above code works. Just out of curiosity to learn the language, can we do this in list comprehension format?
I am at this, so sorry if I miss something.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…