I’m trying to make a shortest code for input with condition as much as possible.
Condition: the number should be greater than 0.
Input: first number determines number of next inputs.
For example:
4
1
-2
3
-4
So i want to append to list() only 1 and 3.
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’m wondering if it can be shorter
I had idea, but it was not working as i expected – “syntax error”:
n=int(input())
t=[x=int(input()) for x in range(n) if(x)>0)]
print(t)
EDIT: forgot. I’m using python3.1…
Here is one way of doing it: