lundi 2 mars 2015

Create Python List using a True/False expression check?

Looking to generate a Python list that uses an if statement to check whether a number is even or odd. If even take the List value as '1' and if odd take the list value as '0'.


Progress to date:


List1 = [x for x in range(0,99) if x % 2 == 0]


However, this only generates a list of even numbers. When I change the expression to add an else check I get a syntax error. Any help appreciated.


List1 = [1 for x in range(0,99) if x % 2 == 0 else 0]


Aucun commentaire:

Enregistrer un commentaire