#!/usr/bin/python
x = [0, 2, 1, 1, 1, 1, 3, 3, 5, 1, 1, 4, 1, 2, 1, 2, 2, 2, 1, 7, 2, 1, 0, 3, 1, 1, 2, 0, 1, 0, 1, 1]
y = [1 for z in x if z > 0]
#WANT TO DO
#y = [1 for z in x if z > 0 else 0]
I want to do both an if
statement and an else
statement within a list comprehension in Python. How can I do this?
I figured out y = [int(bool(z)) for z in x]
, but i was wondering if you could do both an if
and an else
statement in a list comprehension.