Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober 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 Now×
Skip to content
World desk3 min

Python Tuples: How to Create, Access, Unpack, and Use Them

Python tuples are comma-formed, immutable sequences. Learn how to create, access, unpack, and use them as dictionary keys when their contents are hashable.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A Python tuple is an ordered, immutable sequence: after creation, you cannot replace or remove its element references. Tuples are formed by commas, accessed with familiar sequence operations, and often used for fixed collections of values or multiple-value assignment. A tuple can serve as a dictionary key only when every value it contains is hashable.

What is a tuple in Python?

The Python 3.12.15 built-in types reference describes tuples as “immutable sequences, typically used to store collections of heterogeneous data.” In practical terms, a tuple keeps values in order and lets you read them, but does not let you change which objects occupy its slots after it has been created. See the Python built-in types reference.

Tuples are useful for fixed records, such as a point with an x- and y-coordinate. Lists are mutable sequences, better suited to collections whose contents need to be added, removed, or replaced.

Feature Tuple List
Can the sequence’s element references be changed? No Yes
Common role Fixed group of values or record Collection that may change
Creation syntax Comma-separated values, often shown in parentheses Comma-separated values in square brackets
Mutation methods No methods to add, remove, or replace elements Methods include append(), remove(), and pop()

How do you create a tuple?

The comma forms a tuple; parentheses commonly group tuple values, but are not what makes them a tuple. The Python 3.14.8 tutorial puts it simply: “A tuple consists of a number of values separated by commas, for instance:” See Python’s Data Structures tutorial.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
empty = ()
single = (42,)
pair = 1, 2

empty is an empty tuple. The comma in (42,) is essential: (42) is just the integer 42 grouped by parentheses. In pair, the comma creates the tuple, and parentheses can be omitted because the assignment syntax permits it. Parentheses are still useful when they make a longer expression easier to read.

You can also create a tuple from an iterable with the built-in tuple(), for example tuple([1, 2]). An empty call, tuple(), makes an empty tuple.

How do you access tuple elements?

Tuples support sequence operations: zero-based indexing, slicing, iteration, membership tests, concatenation, repetition, and len().

point = (4, 7)
first = point[0]       # 4
first_two = point[:2]  # (4, 7)
contains_four = 4 in point  # True
size = len(point)      # 2

Index 0 refers to the first element. A slice such as point[:2] returns a new tuple containing the selected elements; it does not alter the original. You can loop over values with for value in point:, combine tuples with +, or repeat one with *.

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

The two built-in tuple methods are count(value), which returns the number of occurrences, and index(value), which returns the position of the first matching value. Calling index() when the value is absent raises ValueError.

values = (3, 5, 3)
values.count(3)  # 2
values.index(5)  # 1
values.index(9)  # raises ValueError

How do you unpack a tuple?

Unpacking assigns tuple items to variables in one statement. The number of receiving targets must match the number of values unless you use a starred target.

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

# x is 4; y is 7

Unpacking is also useful when a function returns multiple values: a function can return a tuple, then its caller can assign the elements to separate variables. The Python tutorial documents tuple packing and sequence unpacking in its data structures guide.

Are tuples mutable?

A tuple is immutable, but that describes its element references—not necessarily the objects referenced by its elements. If a tuple contains a mutable object such as a list, you cannot rebind the tuple slot, but you can modify the list itself.

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.
items = ("labels", ["draft"])
items[1].append("published")  # allowed
# items[1] = []               # TypeError: cannot reassign a tuple item

After the append, the tuple still refers to the same list, whose contents have changed. This is shallow immutability, not deep immutability.

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

Can tuples be used as dictionary keys?

Yes, if every value inside the tuple is hashable. A tuple of numbers such as (4, 7) can be a dictionary key. A tuple containing a list cannot, because lists are unhashable.

coordinates = {(4, 7): "point"}  # valid

invalid_key = (4, [7])
# data = {invalid_key: "point"}  # TypeError: unhashable type: 'list'

This rule applies to nested contents too: every contained value must be hashable for the tuple itself to be hashable. Python’s glossary explains hashability and its relationship to dictionary keys.

When should you use a named tuple?

If a tuple represents a record and expressions such as person[0] are unclear, named fields can make code easier to understand. Python’s built-in types reference notes that a named tuple may be more appropriate when access by name is clearer than access by index. The collections.namedtuple() factory creates tuple subclasses with named fields; see the built-in types reference.

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
Crashes, No Sound, or Screen Glitches?Free driver scan
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.