☰

Python Tuple Tutorial with Examples

Python Tuple Tutorial for Beginners

Some information should not change once you have written it. The latitude and longitude of Kolkata. The days of the week. The red, green and blue values of a colour. Python has a data type made for exactly these cases: the tuple. It holds a group of values together, in order, and then locks them.

A tuple looks a lot like a list, and it is the list’s quieter cousin. The big difference is that a tuple cannot be changed after it is created. That sounds like a limit, but it is a useful promise. When you see a tuple in code, you know its contents will not shift under you. Python also uses tuples everywhere behind the scenes, for example when a function returns two values at once.

In this tutorial, we explain what a tuple is and how to create one, including the one item tuple that trips up many beginners. We cover repeating, nesting, slicing, and what immutable really means, including a trap where a tuple appears to change. Then we go through the tuple operations: length, count, index, conversion, comparison and membership. We add unpacking, a comparison with lists, common mistakes and the full program. Every output on this page comes from a real run on Python 3.13.

What is a Python Tuple?

A tuple is an ordered collection of values written in round brackets, with commas between the values. Here is what defines it:

  • Ordered. The values keep the order you gave them.
  • Indexed. You reach each value by position, starting at 0.
  • Immutable. You cannot change, add or remove values after the tuple is created.
  • Allows duplicates. The same value can appear more than once.
  • Can mix types. Numbers, text and other objects can sit in one tuple.

Lists use square brackets and can be changed. Tuples use round brackets and cannot.

How to Create a Tuple in Python?

python
tup1 = (1, 2, 3, 6, 7, 8)
tup2 = ("green", "yellow", "red")
tup3 = "green", "red", "yellow"
print(tup1, tup2, tup3)
output
(1, 2, 3, 6, 7, 8) ('green', 'yellow', 'red') ('green', 'red', 'yellow')

Notice that tup3 has no brackets at all. It is the comma that makes a tuple, not the brackets. Brackets are only there to make it easier to read. Writing values separated by commas is called tuple packing.

An Empty Tuple

python
empty = ()
print(empty)
output
()

A Tuple With One Item

This is the most common beginner mistake. A single value in brackets is not a tuple. You need a comma after it.

python
single = (15,)
not_a_tuple = (15)
print(single, type(single), type(not_a_tuple))
output
(15,) <class 'tuple'> <class 'int'>

The brackets in (15) only group a number, so it stays an integer.

Using the tuple() Function

python
print(tuple([1, 2, 3]))
output
(1, 2, 3)

Repeating Tuples N Times

The star repeats whatever sits before it. With a plain bracketed string, you get one long string. Add a comma and you get a tuple of repeated items.

python
print(("stats") * 4)
print(("stats",) * 4)
output
statsstatsstatsstats
('stats', 'stats', 'stats', 'stats')

Nested Tuples in Python

A tuple can hold other tuples. The result keeps its layers. It is not flattened.

python
a = (0, 4, 8, 12)
b = ("tuple", "python")
nested = (a, b)
print(nested)
print(nested[1][0])
output
((0, 4, 8, 12), ('tuple', 'python'))
tuple

Use one index per layer. nested[1] is the second inner tuple, and nested[1][0] is the first item inside it.

If you do want a single flat tuple, join them with a plus sign instead:

python
a = (0, 4, 8, 12)
b = ("tuple", "python")
print(a + b)
output
(0, 4, 8, 12, 'tuple', 'python')

Immutable Tuple in Python

Try to change an item and Python refuses:

python
tup = (0, 1, "x", "y")
try:
    tup[0] = 4
except TypeError as error:
    print("TypeError:", error)
output
TypeError: 'tuple' object does not support item assignment

To get a changed version, build a new tuple. Joining and slicing both create new tuples and leave the original as it was.

The Trap: A Tuple Can Hold a List

Immutable means the tuple cannot swap one item for another. But if an item is itself a list, that list can still change:

python
mixed = (1, [2, 3])
mixed[1].append(4)
print(mixed)
output
(1, [2, 3, 4])

The tuple still holds the same list. The list changed inside it. Keep this in mind if you rely on a tuple being fully fixed.

Slicing a Tuple in Python

Slicing works exactly like it does for strings and lists. The pattern is tuple[start:end:step]. The start is included, and the end is not.

python
test = (0, 2, 4, 5, 6, 7, 8, 9, 12)
print(test[1:])
print(test[2:5])
print(test[::-1])
print(test[-3:-1])
output
(2, 4, 5, 6, 7, 8, 9, 12)
(4, 5, 6)
(12, 9, 8, 7, 6, 5, 4, 2, 0)
(8, 9)

The reverse slice, with a step of minus one, goes backward through the whole tuple.

Tuple slice examples
Slice Meaning Result
test[1:] From position 1 to the end (2, 4, 5, 6, 7, 8, 9, 12)
test[2:5] Positions 2, 3 and 4 (4, 5, 6)
test[::-1] Everything, backward (12, 9, 8, 7, 6, 5, 4, 2, 0)
test[-3:-1] From the 3rd last to before the last (8, 9)

Tuple Operations in Python

Deleting a Tuple

You cannot delete one item from a tuple. You can delete the whole tuple with del.

python
gone = (0, 1, "a", "b")
del gone
try:
    print(gone)
except NameError as error:
    print("NameError:", error)
output
NameError: name 'gone' is not defined

The error names the tuple that was deleted. If you only want to remove one item, convert to a list, remove it, and convert back, or build a new tuple by slicing.

Length of a Tuple

python
print(len((1, 2, 5, "testing", "length", "of", "tuple")))
output
7

count()

count tells you how many times a value appears.

python
counts = (0, 1, 2, 3, 1, 2, 4, 5, 6, 1, 2, 1)
print(counts.count(1))
output
4

index()

index gives the position of the first match. If the value is missing, it raises a ValueError.

python
words = (1, 2, 5, "a", "python", "tuple")
print(words.index("python"))
try:
    words.index("zzz")
except ValueError as error:
    print("ValueError:", error)
output
4
ValueError: tuple.index(x): x not in tuple

These two, count and index, are the only methods a tuple has. That is because it cannot change, so it has no append, remove or sort.

Converting Between Lists, Strings and Tuples

The conversion only happens when you call tuple():

python
lis1 = [1, 2, 4, "a", "b", "c", "e"]
print(lis1)
print(tuple(lis1))
output
[1, 2, 4, 'a', 'b', 'c', 'e']
(1, 2, 4, 'a', 'b', 'c', 'e')

A string turns into a tuple of its characters:

python
print(tuple("python tuple"))
output
('p', 'y', 't', 'h', 'o', 'n', ' ', 't', 'u', 'p', 'l', 'e')

To change a tuple, go through a list and back:

python
tup = (0, 1, "x", "y")
as_list = list(tup)
as_list[0] = 4
print(tuple(as_list))
output
(4, 1, 'x', 'y')

Comparing Tuples

The old cmp() function was removed in Python 3. Use the comparison signs, or min and max. Python compares tuples item by item from the left, and the first difference decides. If one tuple is a prefix of the other, the shorter one is smaller.

python
t1 = (1, 2, 3, 4, 5)
t2 = (1, 2, 3, 4, 5, 6)
print(min(t1, t2), max(t1, t2))
print(t1 < t2, t1 == t2, (1, 9) < (2, 0))
output
(1, 2, 3, 4, 5) (1, 2, 3, 4, 5, 6)
True False True

The last comparison is True because 1 is less than 2, and that first difference settles it. The 9 and 0 never matter.

Checking for an Element in a Tuple

python
check = (1, 2, 3, "a", "b", "c")
print(3 in check, "x" in check, "x" not in check)
output
True False True

Packing and Unpacking

Unpacking is where tuples shine in real code. You can pull the values out into separate names in one line. The number of names must match the number of values, unless you use a star to collect the rest.

python
point = (3, 5)
x, y = point
print(x, y)
x, y = y, x
print(x, y)
first, *rest = (10, 20, 30, 40)
print(first, rest)
output
3 5
5 3
10 [20, 30, 40]

The second line swaps two values with no temporary variable. This works because Python packs the right side into a tuple and then unpacks it. Functions that return two values are really returning one tuple.

Why Use a Tuple Instead of a List?

  • Safety. The values cannot be changed by mistake.
  • Signals intent. A tuple tells the reader this group is fixed.
  • Dictionary keys. A tuple can be a dictionary key, but a list cannot, because a list can change.
  • Slightly lighter. A tuple generally uses less memory than a list of the same items.
python
locations = {(22.57, 88.36): "Kolkata"}
print(locations[(22.57, 88.36)])
output
Kolkata
Tuple compared with list
Point Tuple List
Brackets Round, ( ) Square, [ ]
Changeable No Yes
Methods Only count and index Many, such as append, remove and sort
Use as a dictionary key Yes, if its items are unchangeable No
Best for Fixed groups, such as coordinates Collections that grow or change

Common Mistakes to Avoid

  • Forgetting the comma in a one item tuple. (15) is an integer. Write (15,).
  • Trying to change an item. It raises a TypeError. Build a new tuple instead.
  • Assuming a tuple is fully frozen. A list inside it can still change.
  • Using cmp(). It does not exist in Python 3.
  • Capital P in Print. Python is case sensitive, so use print.
  • Mismatched names when unpacking. Two names for three values raises a ValueError.
  • Expecting a tuple to flatten when nested. It stays layered. Use plus to join.

Try It Yourself: The Full Program

The program below holds every example in this tutorial. Copy it into a file, run it, and compare your output with what you see above.

python
import sys
print("Python", sys.version_info.major, sys.version_info.minor)

# Creating tuples
tup1 = (1, 2, 3, 6, 7, 8)
tup2 = ("green", "yellow", "red")
tup3 = "green", "red", "yellow"
empty = ()
single = (15,)
not_a_tuple = (15)
print(tup1, tup2, tup3)
print(empty, single, type(single), type(not_a_tuple))
print(tuple([1, 2, 3]))

# Repeating
print(("stats") * 4)
print(("stats",) * 4)

# Nested tuples
a = (0, 4, 8, 12)
b = ("tuple", "python")
nested = (a, b)
print(nested)
print(nested[1][0])
print(a + b)

# Immutable
tup = (0, 1, "x", "y")
try:
    tup[0] = 4
except TypeError as error:
    print("TypeError:", error)

# A tuple can hold a list, and the list can change
mixed = (1, [2, 3])
mixed[1].append(4)
print(mixed)

# Slicing
test = (0, 2, 4, 5, 6, 7, 8, 9, 12)
print(test[1:])
print(test[2:5])
print(test[::-1])
print(test[-3:-1])

# Operations
print(len((1, 2, 5, "testing", "length", "of", "tuple")))
counts = (0, 1, 2, 3, 1, 2, 4, 5, 6, 1, 2, 1)
print(counts.count(1))
words = (1, 2, 5, "a", "python", "tuple")
print(words.index("python"))
try:
    words.index("zzz")
except ValueError as error:
    print("ValueError:", error)

# Converting
lis1 = [1, 2, 4, "a", "b", "c", "e"]
print(lis1)
print(tuple(lis1))
print(tuple("python tuple"))
as_list = list(tup)
as_list[0] = 4
print(tuple(as_list))

# Comparing
t1 = (1, 2, 3, 4, 5)
t2 = (1, 2, 3, 4, 5, 6)
print(min(t1, t2), max(t1, t2))
print(t1 < t2, t1 == t2, (1, 9) < (2, 0))

# Membership
check = (1, 2, 3, "a", "b", "c")
print(3 in check, "x" in check, "x" not in check)

# Packing and unpacking
point = (3, 5)
x, y = point
print(x, y)
x, y = y, x
print(x, y)
first, *rest = (10, 20, 30, 40)
print(first, rest)

# A tuple can be a dictionary key, a list cannot
locations = {(22.57, 88.36): "Kolkata"}
print(locations[(22.57, 88.36)])

# Deleting a whole tuple
gone = (0, 1, "a", "b")
del gone
try:
    print(gone)
except NameError as error:
    print("NameError:", error)

A tuple is a small idea with a clear job: group values and lock them. Run the examples, try to break them, and watch the error messages, which teach as much as the successes. Once tuples feel natural, move on to dictionaries and sets to complete the main Python data types.

Popular Tags:

online python programming course online training courses for python python training course online python training online Python Tuples Tutorials with Examples