October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober 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 a List in Python

Assign list items to variables, capture a variable-length remainder with *rest, expand iterables into a new list, or unzip pairs with zip(*pairs).
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use first, second = values to assign a list’s items to variables from left to right. The number of targets must match the number of items unless you use one starred target, such as first, *rest = values, to collect a variable-length remainder. Python also uses * to expand iterables into a new list and to unzip rows with zip(*rows).

Assign list items to variables

In an assignment, Python unpacks the iterable on the right into the targets on the left, in order. The right-hand value does not have to be a list, but it must provide the required number of items. The commas define the targets; parentheses or square brackets can group them.

As an Amazon Associate I earn from qualifying purchases.

values = [10, 20]
first, second = values

# first == 10
# second == 20

With two targets, the iterable must contain exactly two items. If it contains fewer or more, Python raises a ValueError. For fixed-size data, make the target count reflect the expected shape of the input.

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

Collect a variable-length remainder with *

Put one starred target on the left to collect the items not assigned to the other targets. The starred target always receives a list, which can be empty.

values = [10, 20, 30, 40]
first, *middle, last = values

# first == 10
# middle == [20, 30]
# last == 40

The starred target can appear at the start, middle, or end of the target list. Every non-starred target still needs an item; only the starred target can absorb a remainder of zero or more items.

head, *tail = [10]

# head == 10
# tail == []

Expand values into a new list

In a list display, prefix an iterable expression with * to insert its elements into the new list. This builds a new list and does not mutate the source lists.

left = [1, 2]
right = [3, 4]
combined = [*left, *right]

# combined == [1, 2, 3, 4]

Each expanded expression must be iterable. Trying to expand a non-iterable value, such as an integer, raises an exception.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Unzip rows of pairs with zip(*pairs)

If a sequence contains pairs, zip(*pairs) passes each pair to zip as a separate input. The result groups the first items together and the second items together:

pairs = [(1, "a"), (2, "b")]
numbers, letters = zip(*pairs)

# numbers == (1, 2)
# letters == ('a', 'b')

The columns returned by zip are tuples. Ordinary zip stops as soon as its shortest input is exhausted, so unequal input lengths can silently leave later items unused. If the inputs are expected to be equal in length, use strict=True to raise a ValueError when they are not. If you want to pad shorter inputs instead, use itertools.zip_longest().

numbers, letters = zip(*pairs)

# For parallel iterables that must have equal lengths:
for number, letter in zip(numbers_source, letters_source, strict=True):
    ...

Choose the right unpacking form

What you want Use Key behavior
Assign a fixed number of items to names a, b = values Requires exactly two items.
Assign some items and capture the remainder head, *rest = values rest is a list and may be empty; all non-starred targets still need items.
Insert iterable contents into a new list [*values] Requires an iterable and creates a list display.
Turn paired rows into columns zip(*pairs) Produces tuples; ordinary zip truncates to its shortest input.

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
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair 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.