converting extracted list items to a list in python

I will first declare an empty list, append items to it and at last show the list:

en = []
import numpy as np
x = [*range(1, 101)]
for n in x:
    if np.mod(n, 2) == 0:
        en.append(n)

print(f'Even numbers between 0 and 101 are: {en}') 

Is the long version of:

print(f'Even numbers between 0 and 101 are: {[x for x in list(range(1,101)) if x % 2 == 0]}')

Actually numpy is usless, python just has modulus operator %

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top