# A Sieve of Eratosthenes

I have written this Sieve of Eratosthenes. This is a bit of software that will find all of the prime numbers between 2 and n, where n is a user-defined value.

The code does seem quite bloated and any help at making it more elegant would be great. I know that I am not very good at list comprehensions and if it is possible to utilise them more in this situation I would love to know how.

def sieve_of_eratosthenes(n):
"""
Create a Sieve of Eratosthenes to find all of the prime
numbers between 2 and n
"""
n += 1

num_list = []
[num_list.append(i) for i in range(2, n)]

test = 0
while test < len(num_list):
for i, x in enumerate(num_list):
if num_list[i] % num_list[test] == 0:
if num_list[i] != num_list[test]:
del num_list[i]
test += 1
return num_list

• That is not the Sieve of Eratosthenes, see e.g. my remarks here: codereview.stackexchange.com/a/194762. The Sieve of Eratosthenes algorithms computes multiples, not remainders. Jul 20, 2020 at 13:50

Replace the while loop, making it clearer and more pythonic: for test in num_list: