[Solved] How to convert a for loop in list comprehension in Python?


Could you have been thinking of something like this?

def isPrime(n):
    if n < 2: return None
    factors = [ i for i in range(2, n) if n%i == 0 ]
    return True if not factors else False

print( [ i for i in range(1, 100) if isPrime(i) ] )
#PRINTS:
[2,  3,  5,  7,  11, 13, 17, 19, 23, 29, 31, 37, 
 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]

1

solved How to convert a for loop in list comprehension in Python?