Tuple unpacking
Tuple unpacking is assigning the elements of an iterable to multiple corresponding targets in a single operation.
Python How-To
Unpacking is the technique of retrieving the individual items of an iterable and assigning them to corresponding variables.
- How it is used
- Uses unpacking to extract iterable items into named variables in assignments, loops, and function calls.
- How deep it goes
- Explains one-to-one, starred, underscore, and nested unpacking through examples and an exercise.
- Examples
- Uses task records, numbered prime values, returned statistics, and a nested-list exercise.
- What you can do after
- Apply it in Python to name iterable items, handle unwanted values, and unpack function results.
Pandas Workout: 200 exercises to make you a stronger data analyst
Tuple unpacking assigns the elements of an iterable to multiple variables in one assignment.
- How it is used
- Uses filename metadata to create city and state fields while loading multiple CSV files into pandas DataFrames.
- How deep it goes
- Explains the two-element list returned by split and applies direct assignment, without extending into broader unpacking patterns.
- Examples
- Uses `new+york,ny.csv` to extract city and state before normalizing and assigning them as DataFrame columns.
- What you can do after
- Apply it in Python to capture multiple values from a split filename during batch CSV loading.
Python Workout, Second Edition
Unpacking is a Python assignment operation that extracts elements from an iterable into separate targets.
- How it is used
- Uses unpacking to capture related values from enumerate, split input, and comprehension iterators.
- How deep it goes
- Demonstrates direct assignment in loops, functions, and comprehensions, while leaving advanced pattern matching mostly unelaborated.
- Examples
- Uses alphabet iteration, calculator input parsing, and alphabet-to-number dictionary comprehensions.
- What you can do after
- Apply unpacking via enumerate and str.split to write compact loops, parsers, and comprehensions.
Read how Python Workout, Second Edition explains tuple unpacking →
Also covered in
- Tiny Python Projects: Learn coding and testing with puzzles and games
- The Quick Python Book, Fourth Edition
What to know first
- tupleA tuple is a finite, ordered collection of values grouped as a single composite value, often with elements of different types.
- listA list is an ordered collection of elements arranged in a sequence.
- loopA loop is a programming-language control-flow construct that repeatedly executes a block of instructions according to an iteration rule or continuation condition.
- MapA map is an associative data structure that stores key-value associations and supports retrieving values by key.
Related concepts
- tupleuses · A tuple is a finite, ordered collection of values grouped as a single composite value, often with elements of different types.
- enumerateuses · enumerate is an iteration utility that yields pairs containing sequential indices and corresponding items from an iterable, optionally starting at a specified index.
- loopuses · A loop is a programming-language control-flow construct that repeatedly executes a block of instructions according to an iteration rule or continuation condition.
- iterableuses · An iterable is an object or value that provides or supports a protocol for producing its elements sequentially.
- leading underscore conventionused by · The leading underscore convention prefixes an identifier with an underscore to signal intended internal or non-public use without enforcing access restrictions.
- assignmentuses · Assignment is a programming-language operation that binds or stores an evaluated value in a variable, property, or other assignable location.
Questions about tuple unpacking
What is tuple unpacking?
Tuple unpacking is assigning the elements of an iterable to multiple corresponding targets in a single operation.
Which Manning books explain tuple unpacking?
Python How-To, Pandas Workout: 200 exercises to make you a stronger data analyst and Python Workout, Second Edition.
What should I know before learning tuple unpacking?
Manning books that teach tuple unpacking usually introduce tuple, list, loop and Map first.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.


