vendredi 2 décembre 2016

How to save just the first element that satisfies certain condition in a for loop without stopping it

def sum(x,y):
    R=[x+y,x*y]
    return R
L=[1,2]
P=[]
for i in range(1,1000):
    k=sum(*L)
    P.append(k)
    L=k

I wish to save the first list L whose second coordinate is greater than, say, 20, but i don't want to stop the process of appending to P in the way. So, to add

if L[1]>20:
   u=L
   break

won't work for me.

Aucun commentaire:

Enregistrer un commentaire