Stop 05 · Built-in Data StructuresTopics · 2/9

Tuples

Part of the Python Roadmap

In short

Some data should never change once written, like a location's latitude and longitude or a student's (name, roll number) pair. Tuples lock those values together so nothing edits them by accident.

A tuple is an ordered collection that cannot be changed after it is created. Use it for fixed groups of values like coordinates or a (name, roll_no) pair, and as dictionary keys.

The jaadu slides a card through a laminating machine and holds up the sealed, glossy result proudly.

Think of it like this

A laminated ID card

Your college ID card has your name, roll number and branch printed together and laminated. You can read every detail, but you can't scratch out the roll number and write a new one. A tuple is that laminated card: readable, ordered, fixed.

Watch it happen

List or tuple?

List [ ]

  • Can grow, shrink and change
  • append, remove, sort
  • Cannot be a dict key
  • For collections of similar items

Tuple ( )

  • Fixed after creation
  • Only count and index
  • Hashable, can be a dict key
  • For one record with fixed parts

If the size and values should stay the same, like a coordinate or an RGB colour, use a tuple. If you'll add or remove things, use a list.

The big ideas

What you need to know

Immutable

Once created, you cannot assign to point[0]. Python raises TypeError if you try.

The comma makes it

(5) is just the number 5. A one-item tuple needs a comma: (5,). Even 3, 4 without brackets is a tuple.

Usable as dict keys

Because tuples don't change, they can be hashed. {(19.07, 72.87): 'Mumbai'} is perfectly valid.

Easy to unpack

name, roll = ('Ananya', 42) splits a tuple into separate variables in one line.

See it in code

Walk through the code

python
1student = ("Ananya", 42, "CSE")2print(student[0])        # Ananya3print(len(student))      # 34 5name, roll, branch = student6print(f"{name} ({roll}) studies {branch}")7 8single = ("Ravi",)       # trailing comma needed9print(type(single))      # <class 'tuple'>10 11cities = {(28.61, 77.21): "Delhi", (12.97, 77.59): "Bengaluru"}12print(cities[(12.97, 77.59)])   # Bengaluru13 14try:15    student[1] = 4316except TypeError as err:17    print("Can't change:", err)

1/5Round brackets with commas create a tuple. Indexing and len work just like a list.

A fixed student record, a one-item tuple, tuples as dictionary keys and what happens when you try to edit one.

Save yourself the pain

Common mistakes

Oops

Writing t = (7) and expecting a tuple.

Do this instead

Write t = (7,) or t = 7,. It is the comma, not the brackets, that makes a tuple.

Oops

Trying to use a list like [19.07, 72.87] as a dictionary key.

Do this instead

Lists are mutable and unhashable. Convert it with tuple(coords) first.

Your mission

Railway station lookup

~15 min

Quick check

Which of these creates a tuple with one item?

Go deeper

  1. 1Store a latitude and longitude for your city in a tuple.
  2. 2Return two values from a function and notice that Python packs them into a tuple.
  3. 3Remember that a one-item tuple needs a trailing comma: (5,).

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge