Find Prime numbers using list : List Comprehensive « List « Python






Find Prime numbers using 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



           
       








Related examples in the same category

1.List comprehensions: for and tupleList comprehensions: for and tuple
2.List comprehensions: map and tupleList comprehensions: map and tuple
3.List Comprehensions: a concise way to create lists
4.Define a function to construct listDefine a function to construct list
5.List Comprehensions in buildConnectionString, Step by StepList Comprehensions in buildConnectionString, Step by Step