Parse Many Numbers on One Line
Split, map to int, and unpack cleanly.
Parse Many Numbers on One Line is a free Coding Interview Prep lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Coding Interview Prep learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
One Line, Many Numbers
Contests often pack several values onto a single line, like "3 7 1 9". Your job is to turn that string into numbers you can use.
Split on Spaces
Start with .split(). With no argument it breaks the line on any whitespace and gives you a list of string pieces.
parts = input().split()Strings, Not Numbers Yet
After split() each piece is still a string. "7" is not the same as 7, so you cannot do math on them just yet.
Convert One Piece
Wrap a piece in int() to turn it into a real integer. Do this for every token you need as a number.
x = int("42")Convert Them All with map
Instead of looping, map(int, ...) applies int() to every piece at once, lazily and fast.
nums = map(int, input().split())Make It a List
map returns a lazy object. Wrap it in list() when you need to index, slice, or reuse the values.
nums = list(map(int, input().split()))Unpack Fixed Counts
When you know exactly how many values there are, unpack them straight into named variables in one line.
a, b = map(int, input().split())Three or More Values
Unpacking scales naturally. Reading three numbers is just three names on the left of the equals sign.
a, b, c = map(int, input().split())Floats When Needed
If the values are decimals, swap int for float in the map. Everything else stays the same.
vals = list(map(float, input().split()))Star Catches the Rest
Use a starred name to grab a leading count and collect the remaining numbers into a list.
n, *arr = map(int, input().split())Sum Them Right Away
Because map is iterable, you can feed it straight into sum() without building a list first.
total = sum(map(int, input().split()))Quick Check
One line of code, what does it produce?
Recap: Parse a Line
The combo to remember is map(int, input().split()): split into tokens, convert each to a number, then list or unpack as the problem needs. 🔢
Frequently asked questions
Is the “Parse Many Numbers on One Line” lesson free?
Yes — the full text of “Parse Many Numbers on One Line” is free to read here on the web, and the Coding Interview Prep course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Coding Interview Prep course, upgrade to CoddyKit PRO.
What will I learn in “Parse Many Numbers on One Line”?
Split, map to int, and unpack cleanly. You practise Coding Interview Prep with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Coding Interview Prep?
No prior experience is required. Coding Interview Prep on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Parse Many Numbers on One Line” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Coding Interview Prep lesson?
Yes. Every Coding Interview Prep lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- input() vs sys.stdin Showdown
- Parse Many Numbers on One Line
- Batch Output the Right Way
- Read N Lines and Edge Cases