1

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']
001
  • 13,291
  • 5
  • 35
  • 66
  • 2
    Regex split will work: `import re; re.split('(\W)', input())` [In Python, how do I split a string and keep the separators?](https://stackoverflow.com/a/2136580) – 001 Feb 23 '21 at 19:58
  • 10 is not greater than 10. The question is unclear. – John Gordon Feb 23 '21 at 19:59
  • Does this answer your question? [python - how to split an input string into an array?](https://stackoverflow.com/questions/29577229/python-how-to-split-an-input-string-into-an-array) – Stuart Feb 24 '21 at 00:08

2 Answers2

1

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]
Timur Shtatland
  • 12,024
  • 2
  • 30
  • 47
1

Try something like this

from sys import argv
print ([int(_) for _ in list(argv[1].split(',')) if int(_) > 10])
tandem
  • 2,040
  • 4
  • 25
  • 52