pythonarrayslistinput

What is the most efficient way to read in a list of numbers separated by spaces in python?


Recently, I've been trying some competition coding and a very common thing (at least common from my viewpoint) is to provide a list of integers separated by spaces as an input. For example:

>>> 5 6 7 8 8 1 3 2

I want to be able to read this input in and turn it into a list in Python:

>>> 5 6 7 8 8 1 3 2
[5, 6, 7, 8, 8, 1, 3, 2]

So far, this is the most efficient method I've encountered:

newList = list(map(int, input.split()))
print(newList)

I've also seen a method where you read in the input as a string, then make it a list, but I think map is still more efficient.
Is there any way more efficient than this to read in a list of integers and turn it into a list? Thanks!


Solution

  • try this one

    print("5 6 7 8 8 1 3 2".replace(" ",","))
    print("5,6,7,8,8,1,3,2".replace(","," "))