-4

What I usally do when letting the user enter a list is

myList = []
for i in range(n): #n is a initalized number
    myList.append(int(input()))

This way when the user enters they have to enter 1 item at a time, and need to press enter before they can input the next time. Instead, I want the user to enter something like

1 2 3 4 5 

All the input for the list will be on the same line, and the list will store each number as a item in the list, which will make the list like

[1, 2, 3, 4, 5]

is there a way to do that in Python?

ir6
  • 95
  • 6
  • 2
    Does this answer your question? [Get a list of numbers as input from the user](https://stackoverflow.com/questions/4663306/get-a-list-of-numbers-as-input-from-the-user) – tbhaxor Nov 07 '21 at 02:25
  • not really, I'm trying to find a way to store the items in the list as a int directly, every method out there will store it as a string – ir6 Nov 07 '21 at 02:28

1 Answers1

1

Use the split function:

inpt = "1 2 3 4 5"
lst = inpt.split() -> ['1', '2', '3', '4', '5']

if you want int:

lst = [int(i) for i in inpt.split()] -> [1, 2, 3, 4, 5]
Pedro Maia
  • 2,666
  • 1
  • 5
  • 20