What is the shortest way to convert a string containing integers separated by spaces to a list of integers?

  • input : "-3 1 5 8 10"
  • output : [-3, 1, 5, 8, 10]

I regularly face this situation and I wanted to know if there is a better way than (24 bytes):


1 Answer 1


Using map is way shorter than any of the ways I mentioned. You should do that.

Instead of calling list(...), you should use [*...] (21 bytes):


Or even better, if you switch to python 2, map will always return a list. (18 bytes):


Try it online!

Original Post:

The straightforward way is going to be 26 bytes

[int(n)for n in s.split()]

Try it online!

However, if a tuple is acceptable instead of a list, we could use a trick to shave one byte off leaving us with 25 bytes


Try it online!

This can be shortened even more with the replace function (24):

eval(s.replace(' ',','))

Try it online!

and even more with iterable unpacking (22):

eval(s.replace(*" ,"))

Try it online!

If you truly need a list, you can wrap it in [*...]. This is still shorter than the straightforward way by 1 byte:

[*eval(s.replace(*" ,"))]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.