Converting sequence of integers in list

23 Views Asked by At

Program receive a sequence from integers, divided spaces I dont understand how to convert it in list ?

I've try to do this with split method, but it does not help, because i think, it working only with strings

Input: x = 1 2 3 4 5 6 7 8 9

Output: list_new = [1, 2, 3, 4, 5, 6, 7, 8, 9]

2

There are 2 best solutions below

0
Rayzel On

the input can't be like: x = 1 2 3 4 5 6 7 8 9; it could be something like x = 123456789; in this case, you would think for example to cascade it to string and then split it as you were saying.

0
GreenHood On

The input you're declaring is a string "1 2 3 4 5 6". So to convert it to List[int] you should split each number and convert it to integer, then add each one to the list