0

I want to input in inline

1. input number : 5  
2. 1 5 3 4 2

how to receive input for the number of inputs in python?

I've been tried like this:

num=int(input("inputs_num"))
mlist=[]
for i in range(num):
    n=int(input())
    mlist.append(n)
print(mlist)

I want to input in inline

4
  • 5
    I want to input in lnline - what exactly does that mean?? Commented Jun 3, 2019 at 8:42
  • And BTW, your code seems to already answer the question in the title. Commented Jun 3, 2019 at 8:42
  • I think he wants to print the numbers in line. Commented Jun 3, 2019 at 8:45
  • 2
    If you want to be able to pass 1 5 3 4 2 as input at once rather than one at a time, get rid of the loop, take the input as a string, and then split and map to int. You will end up with a list of ints. Commented Jun 3, 2019 at 8:47

2 Answers 2

2

You want to first get the whole line as a string, then split by spaces into a list, then convert each element into int.

So, the flow would look something like:

"1 5 3 4 2" -> Split -> ['1', '5', '3', '4', '2'] -> Map -> [1, 5, 3, 4, 2]
num=int(input("inputs_num"))
mstr = input().split() # ['1', '5', '3', '4', '2']
mlist=[]
for el in mstr:
  mlist.append(int(el))

Or a more pythonic way would be:

  1. Using list comprehension
num=int(input("inputs_num"))
mlist=[int(i) for i in input().split()]
  1. Using map
num=int(input("inputs_num"))
mlist=list(map(int, input().split()))
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much :) I understand!!
0

simple

i = list(map(int, input("Numbers: ").split()))
print(i)

It will accept multiple integers as input on a single line in Python3

1 Comment

It's a simple solution!! Thank you so much

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.