I am trying to make the code as short as possible for input with a condition.
Condition: the number must be greater than 0.
Input: the first number determines the number of next inputs.
For instance:
4
1
-2
3
-4
So, I want to add only 1 and 3 to the list ().
Here is my code:
n=int(input())
t=[]
for i in range(n):
x = int(input())
if(x>0):
t.append(x)
print(t)
I wonder if this could be shorter? I had an idea, but it did not work as I expected - a "syntax error":
n=int(input())
t=[x=int(input()) for x in range(n) if(x)>0)]
print(t)
EDIT: forgot. I am using python3.1 ...
source
share