- 1

How to take n space separated Integer in a list in python?

If n=3 then input 3 Integer value separated by space

13th Mar 2019, 5:42 AM
Prem Sagar
Prem Sagar - avatar
3 Antworten
+ 8
lst = [int(i) for i in input().split()][:n]
13th Mar 2019, 5:53 AM
portpass
- 1
Yeah its working.. Thanks.
13th Mar 2019, 5:56 AM
Prem Sagar
Prem Sagar - avatar
- 2
Easy solution is just to use the string method split. input: 5 8 0 sgshsu 123 input.split(" ") == ["5", "8", "0", "sgshsu", "123"] #Then they are easy to convert to numeric datatypes, but invalid inputs might raise errors.
13th Mar 2019, 6:06 AM
Seb TheS
Seb TheS - avatar