Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober 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 Scan×
Skip to content
World desk3 min

How to Declare and Use Tuples in Python

Create Python tuples with commas, including the trailing comma required for a singleton. Learn how to unpack, access, and annotate tuples—and when a list is the better choice.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

In Python, commas create tuples; parentheses are commonly added to make the grouping clear. Use () for an empty tuple, (value,) for a one-item tuple, and comma-separated values for a tuple with multiple items. You can access tuple items by index, unpack them into variables, and choose a tuple when the grouped values should not be reassigned as items.

How do you declare a tuple?

A tuple is an ordered sequence whose item bindings cannot be reassigned. Commas are what make comma-separated values a tuple; parentheses are optional in many expressions but make the grouping easier to read.

point = (3, 5)
coordinates = 3, 5
empty = ()
singleton = (3,)

The comma is essential for a one-item tuple. (3) is the integer 3 inside grouping parentheses; (3,) is a tuple. Python’s tutorial and data model document these forms.

Tuples can contain values of different types, as well as other tuples:

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
record = (42, "Ada", True)
nested = (point, (8, 13))

Create a tuple from an iterable

Use the built-in tuple() constructor to make a tuple from an iterable, preserving its iteration order. Calling tuple() without an argument creates an empty tuple.

letters = tuple(["a", "b", "c"])
empty = tuple()

See Python’s built-in tuple documentation for the constructor and tuple operations.

How do tuple packing and unpacking work?

Assigning comma-separated values packs them into a tuple. Assigning a sequence to a matching set of names unpacks its items:

point = 3, 5       # packing
x, y = point       # unpacking

In ordinary unpacking, the number of target names must match the number of sequence items. A starred target collects any remaining items into a list:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
first, *middle, last = (1, 2, 3, 4)
# first is 1; middle is [2, 3]; last is 4

Python’s sequence tutorial describes unpacking and multiple assignment, which combines packing and unpacking.

How do you read tuple values?

Tuples support sequence operations including indexing and slicing. Indexing starts at zero, so point[0] reads the first item in point; slicing selects a range of items and returns a tuple.

point = (3, 5)
first = point[0]       # 3
both = point[0:2]      # (3, 5)

What does tuple immutability mean?

You cannot replace, append, or remove tuple items in place. To represent a different sequence, construct a new tuple. The restriction applies to the tuple’s item bindings, not automatically to every object referenced by those items.

container = ([1, 2], "label")
container[0].append(3)  # the contained list changes
# container[0] = [9]    # TypeError: tuple item assignment is not supported

The list inside container remains mutable, even though the tuple cannot be made to point to a different first item. Python’s tutorial illustrates this distinction.

Free tools Windows power users keep installed

One-click scans. No signup required.

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

Should you use a tuple or a list?

Choose based on whether the sequence itself needs to change, rather than on a rule about the types of its contents.

Need Choose Why
Represent a fixed group or record whose items should not be reassigned Tuple Tuple item bindings are immutable.
Add, remove, or replace entries in the sequence List Lists are mutable.
Use a compound sequence as a dictionary key or set member Tuple, only if every item is hashable A tuple containing an unhashable value, such as a list, cannot be hashed.

Python’s documentation describes tuples as commonly heterogeneous and lists as commonly homogeneous, but that is a convention, not a requirement: a tuple can contain values of the same type, and a list can contain different types. The tutorial and built-in type documentation explain the distinction.

How do you annotate tuple types?

For a fixed pair containing an integer and a string, write tuple[int, str]. For a tuple with any number of integers, write tuple[int, ...].

person: tuple[int, str]
coordinates: tuple[int, ...]

For a tuple type with unpacked components, the typing specification describes starred tuple type syntax for Python 3.11 and newer; its compatibility form uses typing.Unpack. Check the typing specification for the syntax supported by your target Python version.

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

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.