NoobInAllFields
NoobInAllFields

Reputation: 11

Making a list out of input in python

If our input was two numbers in a line. And these numbers were greater than 10. How can I make a list out of them by separating them properly? For example:

Input:10 20

X=List(input()):['1','0',' ','2','0']

But i want to be like this:

X=['10',' ','20']

Upvotes: 1

Views: 99

Answers (2)

Timur Shtatland
Timur Shtatland

Reputation: 12347

Use str.split:

x = input().split()
print(x)
# ['10', '20']

I assume that you do not want the whitespace separator in between (that is, not ['10', ' ', '20']).

If you need to convert the strings to numeric type, use list comprehension:

x = [int(y) for y in input().split()]
print(x)
# [10, 20]

Then you can do numeric operations such as this, which results in a list with a single element, 20:

x = [y for y in x if y > 10]
print(x)
# [20]

Upvotes: 1

tandem
tandem

Reputation: 2238

Try something like this

from sys import argv
print ([int(_) for _ in list(argv[1].split(',')) if int(_) > 10])

Upvotes: 1

Related Questions