The shortest code to enter

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 ...

+3
source share
4 answers

Here is one way to do this:

[x for x in (int(input()) for _ in range(int(input()))) if x > 0]
+6
source
filter(lambda x: x > 0, (int(input()) for i in range(int(input()))))
0
source

.... .:)

. ( ), , :

t = [ x for x in [int(input()) for y in range(input())] if x > 0]
print t

The call range(input())does not need to be broadcast in int, because there will still be an error if rangeit is not transmitted int.

0
source
print sum(max(0,input())for _ in range(input()))
-2
source

All Articles