You just learned that lists can be changed after creation: items added, removed, updated. A tuple looks almost identical to a list, but comes with one key difference: once you create it, you can’t change it. That might sound limiting, but it’s actually the whole point.
What a tuple actually is
A tuple stores multiple values in order, just like a list, but you create it with round brackets instead of square ones.
colors = ("red", "green", "blue")
print(colors)
'''Output:
('red', 'green', 'blue')'''Accessing items
Same indexing rules as a list, starting at 0, with negative indexes counting from the end.
colors = ("red", "green", "blue")
print(colors[0]) # red
print(colors[-1]) # blueThe main difference: tuples are immutable
Try to change an item in a tuple and Python stops you.
colors = ("red", "green", "blue")
colors[0] = "yellow"
# Output
# TypeError: 'tuple' object does not support item assignmentThis is by design. A tuple is meant for data that shouldn’t change once it’s set, coordinates, days of the week, a fixed set of settings.
Why use a tuple instead of a list
If your data isn’t supposed to change, a tuple protects it from being accidentally modified somewhere later in your code. It’s also slightly faster than a list, since Python doesn’t need to keep it flexible. In practice, that speed difference rarely matters for beginner projects, the real reason to reach for a tuple is when you want to guarantee the values stay fixed.
coordinates = (27.7172, 85.3240)Nobody should be able to accidentally overwrite Kathmandu’s latitude halfway through a program. A tuple makes that mistake impossible instead of just unlikely.
A tuple with a single item
This one catches people off guard.
single = ("apple")
print(type(single)) # <class 'str'>, not a tuple!Python treats ("apple") as just a string in brackets, not a tuple. To create a one-item tuple, you need a trailing comma.
single = ("apple",)
print(type(single)) # <class 'tuple'>Easy to forget, and it’s a common source of confusing bugs.
Looping through a tuple
Works exactly like a list.
colors = ("red", "green", "blue")
for color in colors:
print(color)Checking length and membership
colors = ("red", "green", "blue")
print(len(colors)) # 3
print("green" in colors) # True
print("purple" in colors) # FalseConverting between tuples and lists
If you do need to change a tuple’s contents, convert it to a list, make your changes, then convert it back.
colors = ("red", "green", "blue")
colors_list = list(colors)
colors_list.append("yellow")
colors = tuple(colors_list)
print(colors) # ('red', 'green', 'blue', 'yellow')Python Tuple Practice Questions
- Create a tuple with the names of three countries and print the last one using negative indexing.
- What is the output of the following code?
t = (1, 2, 3) t[1] = 10 - Write code that checks whether “Nepal” exists inside a tuple of country names.
- What type does
x = ("hello")actually create? How would you fix it to make x a proper one-item tuple? - Write a program that converts a tuple of numbers into a list, adds one more number to it, then converts it back into a tuple.
- Explain, in your own words, one real situation where you’d choose a tuple over a list.
Frequently Asked Questions
Tuples in Python
