liveBook · Manning

Tuple unpacking

As explained in 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.

Tuple unpacking assigns the elements of an iterable to multiple variables in one assignment. When a filename is split into a city and state, the two resulting strings can be captured directly. section 7.2

Extracting location fields from a filename

The filename `new+york,ny.csv` becomes two values after its path, suffix, and delimiter are removed. Tuple unpacking assigns them to `city` and `state`; later string methods normalize the values before adding them to a DataFrame. section 7.2

city, state = (
    one_filename
    .removeprefix('../data/')
    .removesuffix('.csv')
    .split(',')
)

This compact expression shows the complete transformation from filename to two named variables. section 7.2

How Pandas Workout: 200 exercises to make you a stronger data analyst covers it

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 it assumes
Assumes familiarity with Python assignment, iterable results, and string methods such as split, removeprefix, and removesuffix.
What you can do after
Apply it in Python to capture multiple values from a split filename during batch CSV loading.
How it connects
Connects Python string methods, filename parsing, pandas DataFrames, and method chaining.

Also explained in

Compare how 3 books explain tuple unpacking →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pandas Workout: 200 exercises to make you a stronger data analyst. The section links point to the parts of the book it draws on.