How can I combine a conditional with a for loop in Python?

后端 未结 3 970
囚心锁ツ
囚心锁ツ 2021-01-22 01:08

I have a simple example I\'ve drawn up. I thought it was possible to combine if statements and for loops with minimal effort in Python. Given:

sublists = [numb         


        
3条回答
  •  死守一世寂寞
    2021-01-22 01:35

    Immediately solved this in the interpreter right after I posted.

    for x in ( x for x in sublists if x ):
    

    Not as clean as I'd like, the nested if statement is more readable in my opinion. I'm open to other suggestions if there is a cleaner way.

提交回复
热议问题