0
a,b = map(int, input().split())

In the above code or anything similar, we use split to separate multiple inputs on a single line, typically separated with a space, and assign the results to the variables.

This is very convenient feature, however I am facing a problem:

Say I want to populate a list A with n integers inside of it, It is relatively easy to ask for n to get the list size and populate it using split, BUT! what if the user wrote too many values?

Say I have 3 variables to fill but the user inputted 4, I get the ValueError: too many values to unpack.

Is there any way to limit the user input to n space separated variables that they would write on a single line? i.e: after writing n space separated variables, stop listening for inputs or don't let them type anything more or just disregard whatever comes after that.

Does split have any functionality like that?

I am newly learning python and as I write this, it comes to my mind to try and take whatever the user inputs, put it in a list, slice off whatever elements beyond n, our list size, and assign the remaining values to A, our list. But that sounds like scratching my left ear with my right hand (like the needlessly long way) and it feels, to me at least, that something like that should be included in split or in python somewhere.

I'm a beginner so please keep your answer relatively beginner-friendly/easy to tell what is going on.

Thank you,

Fuzzy.


Note: I am aware that I can take each input on a line and use a for loop in range(n), yes. But the aim here is to use input().split().

1
  • Keep it one-liner while adding proper input validation is a challenge. Commented Nov 9, 2022 at 16:26

2 Answers 2

1

You can catch any additional unwanted values with an asterisk (an underscore typically represents an unused variable):

a, b, *_ = map(int, input().split())

This will place any additional inputs into a list called _. Note that this method requires at least two input values separated by a space.

Sign up to request clarification or add additional context in comments.

5 Comments

What is the asterisk doing? is it like saying put everything in _[] ? Sorry, I'll go search for it. But thank you!!
Yup! The left side of the assignment says, take the first thing and place it in a, take the second thing and place it in b, and then take anything else and place it in a list called _ (that list could be called anything you want).
Omg, I just learned about this unpacking with the asterisk from google and it was all thanks to your answer. Thank you! Python really is powerful and flexible (just saw magic like a,*b,c = list)
Umm, how do I free *_ if I don't need it tho?
You could call del _, although that's not very 'pythonic'. It will eventually get cleaned up by the garbage collector.
0

Your idea of "slicing off" additional inputs can actually be done pretty concisely like this:

a,b = map(int, input().split()[:2])

...of course, there remains the possibility of the user giving a list of inputs that is too short.

1 Comment

So, umm, this is taking the first 2 items in the list of inputs? I'll try and see what would happen with inputs less than 2, but the problem I'm solving gave that n should be at least 2, i guess it was for this purpose? Thank you!

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.