Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

List of every integer except 2 with list comprehensions

Is there a simpler way to achieve this with list comprehensions? I’m new to them and currently trying to improve my coding:

list = []
for k in range(1000):
    if k != num1 and k != num2:
        listToFade.append(k)

basically in ‘list’ you have all the numbers from 0 to 999 except 2

Thanks!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Here is a list comprehension version of your code:

list=[i for i in range(1000) if i not in [num1, num2]]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading