Sitemap
Saurabh Gandhi

Saurabh Gandhi

Python • AI • Tech Learning Writing simple guides that make coding easy for everyone.

Tuples in Python

3 min readMar 1, 2026

--

Tuples are one of the most important built-in data types in Python. They are similar to lists but with one major difference:

  • Tuples are immutable (cannot be changed after creation)
  • Lists are mutable (can be changed)

What is a Tuple?

A tuple is an ordered collection of elements enclosed in parentheses ()

Get Saurabh Gandhi’s stories in your inbox

Join Medium for free to get updates from this writer.

Tuple items are indexed, the first item has index [0], the second item has index [1] etc.

my_tuple = (10, 20, 30)
print(my_tuple)

Characteristics of Tuples

Press enter or click to view image in full size

Example:

t = (10, "Python", 3.14, True)
print(t)

Creating Tuples

# 1. Using parentheses
t = (1,2,3)

# 2. Without parentheses (Tuple Packing)
t = 1, 2, 3
print(t)

#3. Without parentheses (Tuple Packing)
t = (5,)

Accessing Tuple Elements (Indexing)

t = (10,20,30,40)
print(t[0]) #10
print(t[-1]) #40

Tuple Slicing

t = (10, 20, 30, 40, 50)

print(t[1:4]) # (20, 30, 40)
print(t[:3]) # (10, 20, 30)
print(t[::2]) # (10, 30, 50)

Tuple Immutability

You cannot modify elements.

t = (10, 20, 30)
t[0] = 100 # ❌ Error : TypeError: 'tuple' object does not support item assignment

Tuple Packing & Unpacking

  • Packing
data = ("Raghav", 25, "Developer")
  • Unpacking
name, age, role = data
print(name)
  • Using * Operator (Advanced Unpacking)
t = (1, 2, 3, 4, 5)

a, *b, c = t
print(a) # 1
print(b) # [2, 3, 4]
print(c) # 5

Tuple Methods

Tuples have only 2 built-in methods:

  • count() : Returns the number of times a specified value occurs in a tuple
  • index() : Searches the tuple for a specified value and returns the position of where it was found

Example :

t = (1,2,2,3,2)
print(t.count(2)) #3
print(t.index(3)) #3

Why Use Tuple Instead of List?

  • When data should not change
  • Faster performance
  • Used as dictionary keys
  • Safe data structure

Looping Through Tuple

t = (10, 20, 30)

for item in t:
print(item)

Nested Tuples

t = ((1, 2), (3, 4))
print(t[1][0]) # 3

Tuple Operations

t1 = (1, 2)
t2 = (3, 4)

print(t1 + t2) # Concatenation
print(t1 * 3) # Repetition

When NOT to Use Tuple?

  • When data needs frequent modification
  • When you need many built-in methods

Questions

  1. What is a tuple?
  2. Difference between list and tuple?
  3. Why are tuples immutable?
  4. Can a tuple contain mutable objects?
  5. How to create single element tuple?
  6. What is tuple packing and unpacking?
  7. Explain * operator in unpacking.
  8. Difference between shallow copy in list and tuple?
  9. Why tuple is faster than list?
  10. Explain memory optimization in tuples.
  11. How does immutability help in multithreading?
  12. What happens if tuple contains a list inside it?
  13. Explain hashability in context of tuple.
  14. Internal working difference between list and tuple.

--

--

Saurabh Gandhi

Written by Saurabh Gandhi

Python • AI • Tech Learning Writing simple guides that make coding easy for everyone.