Skip to main content

Pythonic Data Structures

Tuple Unpacking and Starred Expressions

0:00
LearnStep 1/3

Pythonic Sequence Unpacking

Basic Unpacking

Python allows you to 'unpack' sequences into individual variables in a single statement. This is most commonly used with tuples but works with any iterable.

python

The Starred Expression (Extended Unpacking)

Introduced in Python 3, the * operator allows you to capture 'everything else' into a list. This is useful when you only care about specific parts of a sequence.

python

Unpacking in Loops

Unpacking is incredibly powerful when iterating over a list of tuples or dictionaries.

python

The Pythonic Swap

Forget using a temp variable to swap two values. Python handles it elegantly using unpacking.

python