Carl groth Carl groth - 7 months ago 18
Python Question

Multiple inputs from one input

I'm writing a function to append an input to a list. I want it so that when you input

280 2
the list becomes
['280', '280']
instead of
['280 2']
.

Answer
>>> number, factor = input().split()
280 2
>>> [number]*int(factor)
['280', '280']

Remember that concatenating a list with itself with the * operator can have unexpected results if your list contains mutable elements - but in your case it's fine.

edit:

Solution that can handle inputs without a factor:

>>> def multiply_input():
...     *head, tail = input().split()
...     return head*int(tail) if head else [tail]
... 
>>> multiply_input()
280 3
['280', '280', '280']
>>> multiply_input()
280
['280']

Add error checking as needed (for example for empty inputs) depending on your use case.