Mega Code Archive

 
Categories / Python / List
 

Find Prime numbers using list

#First build a list of non-prime numbers, using a single list comprehension,  #then use another list comprehension to get the "inverse" of the list,  #which are prime numbers. noprimes = [j for i in range(2, 8) for j in range(i*2, 50, i)] primes = [x for x in range(2, 50) if x not in noprimes] print primes