October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

How to Unpack Tuples in Python

Unpack Python tuples with comma-separated targets, handle extra items with a starred target, and avoid length-mismatch errors.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Assign comma-separated names to the left of an iterable: x, y = (4, 9). Each name receives the value in the corresponding position. Without a starred target, the iterable must contain exactly as many items as there are names.

Unpack a tuple into variables

Put one target on the left for each tuple item:

point = (4, 9)
x, y = point

print(x)  # 4
print(y)  # 9

This is called sequence unpacking in the Python 3.11.16 tutorial. The operation assigns the values by position: the first item goes to x, the second to y. Multiple assignment combines tuple packing on the right with unpacking on the left.

Unpacking works with other iterables, too

The right-hand side does not have to be a tuple. Lists and other sequences can be unpacked the same way, and an iterable such as an iterator can be unpacked when its number of yielded values is known. The Python assignment reference describes assignment from iterables; the Functional Programming HOWTO discusses unpacking known-length iterators.

colors = ["red", "green"]
first, second = colors

first, second = iter(("red", "green"))

Match the number of values and targets

Without a starred target, the number of items must equal the number of targets. A mismatch raises ValueError:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
x, y = (1, 2, 3)  # ValueError: too many values to unpack
x, y, z = (1, 2)  # ValueError: not enough values to unpack

This exact-length rule is specified in the Python assignment reference. If the iterable’s length can vary, use a starred target for the values you do not want to name individually.

Use * to collect remaining values

A single starred target collects the items not assigned to targets before and after it. Python gives those collected items to the starred target as a list:

first, *middle, last = (10, 20, 30, 40)
# first is 10
# middle is [20, 30]
# last is 40

The iterable must still contain at least enough items for the non-starred targets. The starred list can be empty:

first, *middle, last = (10, 40)
# middle is []

The assignment reference documents the minimum-length rule and the list result.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Swap two values with multiple assignment

Unpacking lets you exchange two variables without a temporary variable:

left, right = right, left

The right-hand values are evaluated and packed before they are assigned to the left-hand targets. The Python tutorial describes multiple assignment as a combination of tuple packing and sequence unpacking.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Remember the comma in tuple syntax

Parentheses can group an expression, but a comma is what makes a tuple. For a one-item tuple, include the trailing comma:

single = (7,)  # one-item tuple
not_a_tuple = (7)  # integer 7
empty = ()  # empty tuple

The same singleton tuple can be written 7,. See the expressions reference and built-in types documentation for tuple syntax and behavior.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

When positional unpacking is hard to read

Unpacking is concise when positions are obvious, as with a coordinate pair. For heterogeneous records, however, names can be clearer than positions. The built-in types documentation notes that collections.namedtuple() may be more appropriate when named fields improve readability; ordinary tuples remain useful when positional access is clear.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.