Python Tuple Collection Tutorial

In this tutorial we learn one of Python's immutable collections, called tuples.

We cover how to declare, initialize, access, change and remove items, as well as its constructors and common functions.

What is a tuple?

The tuple data type is one of the sequence, or collection data types in Python, similar to a list. Like the string collection, a tuple is immutable, which means its values cannot change at runtime.

How to declare/initialize a tuple

Like other collections, we initialize a tuple by assigning multiple, comma separated, items to a variable. Where the list uses square brackets to enclose its items, the tuple uses parentheses.

Syntax:
 tuple_name = (value_1, value_2, value_3, ...)
Example: declare / initialize a tuple
 shopping = ("Bread", "Milk", "Cheese", "Eggs")

How to access tuple items

We can access a tuple’s items with the indexer in either standalone format, or as temporary variable in a loop.

How to access tuple items with the indexer

To access an item in a tuple with the indexer we reference its index number inside square brackets. The index must always be an integer.

Syntax:
tuple_name = (value_1, value_2, value_3, ...)

tuple_name[index]
Example: access tuple items by index
shopping = ("Bread", "Milk", "Cheese", "Eggs")

print(shopping[0])

When we run the example above, we see that the first item in the list is printed, “Bread”.

tip A numerical index always starts at number 0, not at number 1.

How to access tuple items in a loop

In general, it will be easier, more efficient and less code to access tuple items in a loop. A for loop is perfect for collections such as a tuple.

Syntax:
tuple_name = (value_1, value_2, value_3, ...)

for x in tuple_name:
    # do something with x

We use “x” as our temporary variable in the for loop above. It represents the index of an item inside the tuple, thereby allowing us access to the value at that index.

Example: access tuple items in a loop
shopping = ("Bread", "Milk", "Cheese", "Eggs")

for item in shopping:
    print(item)

When we run the example above, we see that all the items in the tuple are printed. A loop makes it easy to access items in the tuple.

How to change items in a tuple

A tuple is immutable, which means that once we’ve created a tuple, its values cannot change at runtime.

Example:
shopping = ("Bread", "Milk", "Cheese", "Eggs")

shopping[0] = "Apples"

for item in shopping:
    print(item)

When we run the example above, we’ll get a TypeError.

Output:
 TypeError: 'tuple' object does not support item assignment

How to check if a tuple item exists

To check if an item exists inside the tuple, we use an if..in statement.

Syntax:
tuple_name = (value_1, value_2, value_3, ...)

if value_1 in tuple_name:
    # do something
Example:
shopping = ("Bread", "Milk", "Cheese", "Eggs")

if "Bread" in shopping:
    print("Bread is in the tuple")

The statement above reads: if the item Bread is in the shopping tuple, print a message

How to remove items from a tuple

A tuple is immutable and cannot be changed, including removing items. But, we can delete a tuple completely with the del keyword.

Syntax:
tuple_name = (value_1, value_2, value_3, ...)

del tuple_name
Example: delete a tuple
shopping = ("Bread", "Milk", "Cheese", "Eggs")

del shopping

for item in shopping:
    print(item)

When we run the example above we’ll see a NameError.

Output:
 NameError: name 'shopping' is not defined

The name “shopping” doesn’t exist anymore because we deleted the whole list.

How to create a tuple with the tuple constructor

Python allows us to make a tuple by using the tuple constructor.

Even though we learn about them here, constructors are a concept of classes. We learn more about classes in the object oriented programming lesson on classes and objects .

To create a tuple with the tuple constructor we use the keyword tuple , with the values we want to add in between double parentheses.

Syntax:
 tuple_name = tuple((value_1, value_2, value_3, ...))
Example: create a tuple with the constructor
shopping = tuple(("Bread", "Milk", "Cheese", "Eggs"))

for item in shopping:
    print(item)

Common tuple functions

The following table shows some common functions used with tuples:

FunctionDescription
index()Search tuple for specified value and return position index
len()Returns the number of elements in the tuple
max()If the tuple contains numbers, the highest number will be returned
 If the tuple contains strings, the last item in alphabetical order will be returned
min()If the tuple contains numbers, the lowest number will be returned
 If the tuple contains strings, the first item in alphabetical order will be returned
count()Returns the number of times a specified value occurs in the tuple

Summary: Points to remember

  • A tuple is like a variable that can store multiple separate values.
  • We store tuple values by writing them between parentheses, and separating each value with a comma.
  • We access tuple values with the indexer. That is to say, the index number that corresponds to the item’s location in the tuple.
  • Tuples are immutable. Their values cannot be changed.
  • We check if an item exists in a tuple with an if..in statement.
  • We delete a tuple entirely with the del keyword.