Python Introduction to Tuples
Table of Contents
In the last lesson, we learned about Python Copying Lists. Now let’s work with ordered collections that cannot be changed.
Let’s Talk About a Different Kind of Collection
We already know about lists. A list is a collection of items. We can change it. Add items. Remove items. Sort it. All of that.
But sometimes, we do not want a collection that can change. Sometimes we want a collection that is fixed. That cannot be modified after it is created.
That is what a tuple is.
A tuple is like a list, but it is immutable. Once we create it, we cannot change it. No adding. No removing. No changing items.
Think of it like a sealed envelope. We put things inside. We seal it. Now it is fixed. We can look at what is inside. But we cannot change it without opening a new envelope.
How to Create a Tuple in Python
We create a tuple by putting items inside parentheses ( ), separated by commas.
coordinates = (10, 20)colors = ("red", "green", "blue")mixed = (1, "hello", True, 3.14)That is a tuple. Just like a list, but with parentheses instead of square brackets.
We can also create a tuple without parentheses. Just commas.
point = 5, 10print(point)Output
(5, 10)Python understands that as a tuple. But usually, people use parentheses for clarity.
A Special Case: Single-Item Tuple in Python
If we want a tuple with one item, we need a trailing comma.
single = (5,)print(single)Output
(5,)If we write (5) without the comma, Python thinks it is just the number 5 in parentheses. Not a tuple.
not_a_tuple = (5)print(type(not_a_tuple))Output
<class 'int'>So let’s remember: single-item tuple needs a comma.
Accessing Items in a Python Tuple
We access items just like a list. Using indices.
colors = ("red", "green", "blue")print(colors[0])Output
redprint(colors[1]) # greenprint(colors[-1]) # blueNegative indexing works too.
Slicing a Python Tuple
We can slice a tuple just like a list. It returns a new tuple.
numbers = (10, 20, 30, 40, 50)print(numbers[1:4])Output
(20, 30, 40)Same rules as list slicing. Start inclusive, end exclusive, optional step.
Tuples Are Immutable in Python
This is the key difference. We cannot change a tuple.
colors = ("red", "green", "blue")colors[0] = "yellow" # Error!Output
TypeError: 'tuple' object does not support item assignmentWe cannot add items.
colors.append("yellow") # Error!We cannot remove items.
colors.remove("red") # Error!Tuples do not have these methods. Because they are immutable.
Why Use Tuples in Python?
If tuples cannot change, why use them?
There are several reasons.
First, safety. If we have data that should not change, a tuple guarantees it will not change. That prevents bugs.
Second, speed. Tuples are slightly faster than lists. Because they are simpler.
Third, they can be used as keys in dictionaries. Lists cannot. Because keys must be immutable.
Fourth, they are often used to return multiple values from a function. We saw that earlier.
Fifth, they make our code clearer. When we see a tuple, we know it is not meant to be modified.
Python Tuples: A Real-Life Example
Let’s say we have coordinates. A point in 2D space. That does not change. So a tuple is perfect.
point = (10, 20)x, y = pointprint(f"x = {x}, y = {y}")Output
x = 10, y = 20That is called unpacking. We will cover it in a later article.
Another example: RGB colors.
red = (255, 0, 0)green = (0, 255, 0)blue = (0, 0, 255)Those are fixed. They do not change. So tuples are a good choice.
Tuples Can Hold Anything
Like lists, tuples can hold any type of data. Even other tuples.
nested = ((1, 2), (3, 4), (5, 6))And we can access with multiple indices.
print(nested[0][1])Output
2Looping Through a Python Tuple
We can loop through a tuple just like a list.
colors = ("red", "green", "blue")for color in colors: print(color)Output
redgreenblueChecking If an Item Is in a Tuple
We use in.
colors = ("red", "green", "blue")print("green" in colors)Output
TrueLength of a Python Tuple
We use len().
colors = ("red", "green", "blue")print(len(colors))Output
3Common Mistakes with Python Tuples
Mistake 1: Forgetting the Comma for Single-Item Tuple
single = (5)print(type(single)) # <class 'int'>So what do we do? We add the comma.
single = (5,)Mistake 2: Trying to Change a Tuple
colors = ("red", "green")colors[0] = "blue" # Error!Let’s remember, tuples are immutable. If we need to change it, we use a list instead.
Mistake 3: Confusing Tuples and Lists
my_list = [1, 2, 3] # listmy_tuple = (1, 2, 3) # tupleThey look similar. But lists use square brackets, tuples use parentheses. And lists are mutable, tuples are not.
Mistake 4: Using Parentheses for Grouping and Thinking It’s a Tuple
x = (5)That is just the number 5. Not a tuple. To make it a tuple, we need a comma.
Next, we will learn about Python Tuple Operations and work with tuple values.