How to take multiple input in python for list and tuple by comprehension

64 Views Asked by At

How to take multiple input in a single line for tuple and list ?

a= list(x = int(input()) for x in range(n)  if x > 0)

''' it has an error , how can I solve it and use similar code for tuple '''
1

There are 1 best solutions below

0
On

You can use filter on your comprehension, like this:

a = list(filter(lambda x: x>0, (int(input()) for _ in range(n))))

For n = 5 and the input:

1
0
3
-10
4

it would print:

[1, 3, 4]