Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 06

Tuple Deep Dive — Immutable Sequences

Create Python tuples safely, avoid the single-element trap, convert from lists and strings, unpack coordinates, and know when tuples beat lists.

Intermediate22 min

What you will learn

Immutable · Ordered · Hashable · Fixed Data

A tuple is an ordered collection just like a list, but it cannot be modified after creation. Use tuples for data that should stay constant — GPS coordinates, RGB colors, database records, and function return values.

Property Tuple behavior
Ordered Yes — index and slice work
Mutable No — no item assignment
Methods Only index() and count()
Hashable Yes (when all items are hashable)

Memory formula: Create → Access → Convert → Protect

After File 05 — Nested Lists, you know how to structure 2D data with lists. Tuples protect fixed records that should not change accidentally.

Basics: tuple — Immutable Ordered Collection.

Learning roadmap

Create access convert protect — tuple learning path

1. Tuple vs list

Although they look similar, their behavior is very different.

List mutable — append and sort allowed

Mutable

Tuple immutable — index and count only

Immutable

Feature List Tuple
Syntax [] () or comma
Mutable Yes No
Add items append() Not allowed
Delete items remove() Not allowed
Best for Dynamic data Fixed records

2. Creating tuples

Different ways

python
t1 = ()
t2 = tuple()
t3 = (10, 20, 30)
t4 = 10, 20, 30

t1 and t2 are empty tuples. t3 and t4 both create (10, 20, 30).

Empty tuple vs parenthesized vs comma-separated creation

Important: Parentheses are optional. The comma actually creates the tuple.

3. The single-element trap

This is one of the most common interview questions.

Wrong (10) is int vs correct (10,) is tuple
python
not_tuple = (10)
real_tuple = (10,)
also_tuple = 10,

print(type(not_tuple))   # int
print(type(real_tuple))  # tuple

Remember: () groups expressions. , creates tuples.

4. Converting between lists and tuples

Python provides two built-in constructors.

List to tuple conversion flow
python
print(tuple([10, 20, 30]))
print(tuple("PYTHON"))
print(tuple(range(5)))
print(list((1, 2, 3)))

Output:

text
(10, 20, 30)
('P', 'Y', 'T', 'H', 'O', 'N')
(0, 1, 2, 3, 4)
[1, 2, 3]
Function Result
tuple(list) List → tuple
list(tuple) Tuple → list
tuple(str) Characters as tuple
tuple(range()) Number sequence

5. Indexing and slicing

Tuples follow exactly the same indexing rules as lists — but no assignment.

Tuple indexing t[0] t[-1] t[1:3]
python
t = (10, "RS", 34.56, True)

print(t[0])     # 10
print(t[-1])    # True
print(t[1:3])   # ('RS', 34.56)

6. Immutability in action

Trying to change a tuple causes an error.

python
t = (10, 20, 30)
t[0] = 99  # TypeError: 'tuple' object does not support item assignment
Tuple cells locked — cannot modify

Key idea: You may read values, but not replace them.

7. Can tuples contain mutable objects?

Yes! The tuple is immutable, but objects inside it may not be.

python
record = (
    100,
    "RS",
    {"Python": 95}
)

record[2]["OS"] = 90
print(record)  # (100, 'RS', {'Python': 95, 'OS': 90})
Tuple cannot replace slot but inner dict is mutable

You cannot do record[2] = {} — that would replace a tuple slot. But the dict object at that slot can still be modified. See Immutable Types for the full rule.

8. Packing and unpacking

Packing puts multiple values into one tuple. Unpacking assigns them to separate variables.

Packed tuple unpacked into lat and lon
python
# Packing
point = 12.9716, 77.5946

# Unpacking
lat, lon = point
print(lat, lon)

Star unpacking

python
first, *middle, last = (1, 2, 3, 4, 5)
print(first)   # 1
print(middle)  # [2, 3, 4]
print(last)    # 5

*middle collects remaining items into a list.

When to choose a tuple

  • Return multiple values from a function
  • Dictionary keys (must be hashable)
  • Protect data from accidental in-place edits
  • Slightly smaller and faster than lists for fixed data

Mini project — GPS coordinates

Store latitude and longitude as a fixed tuple; unpack when passing to a function.

python
def distance_from_origin(point):
    lat, lon = point
    return (lat ** 2 + lon ** 2) ** 0.5

location = (12.9716, 77.5946)
print(distance_from_origin(location))
GPS coordinate tuple with lat and lon

The tuple keeps the coordinate pair fixed — you cannot accidentally append a third value like you could with a list.

One-page revision sheet

Task Syntax
Empty tuple () or tuple()
Single-item tuple (10,) or 10,
List → tuple tuple(my_list)
Tuple → list list(my_tuple)
Read element t[i]
Slice t[start:end]
Unpack a, b = t
Star unpack first, *rest = t

Common interview mistakes

Mistake 1 — (10) is not a tuple:

python
type((10))    # int — wrong
type((10,))    # tuple — correct

Mistake 2 — assuming tuple means everything inside is frozen:

python
t = ([1, 2],)
t[0].append(3)  # works — inner list is mutable

Correct: Immutability applies to tuple slots, not necessarily to nested mutable objects.

Practice questions

Test yourself before File 07 — Tuple Methods:

  1. What is type((10)) vs type((10,))?
  2. How do you convert a list to a tuple?
  3. Why does t[0] = 5 fail on a tuple?
  4. Can a tuple contain a dictionary? Can you modify that dictionary?
  5. How do you unpack (lat, lon) into two variables?

Knowledge check

Q1: type((10)) vs type((10,))?
Answer: int vs tuple — trailing comma required for single-item tuples.

Q2: Convert list to tuple?
Answer: tuple(my_list).

Q3: Why does t[0] = 5 fail?
Answer: Tuples are immutable — item assignment is not supported.

Q4: Tuple with a dict inside — can you modify the dict?
Answer: Yes — the dict is mutable; you just cannot replace the tuple slot with a different object.

Q5: Unpack (lat, lon)?
Answer: lat, lon = point — one variable per element, left to right.

Loading playground…