Skip to main content

Tuple

A tuple stores several items in one variable.

It is one of the 4 built-in types Python offers for collections of data. The others are List, Set and Dictionary, and each has its own qualities and uses.

What sets a tuple apart is that it is ordered and unchangeable.

You write one using round brackets.

Example

Building a simple tuple:

Prompt
Create a tuple with three fruit names and print it.
Output: complete Python program

Intro

main.py
mytuple = ("apple", "banana", "cherry")

Tuple Items

Three things define tuple items: they keep their order, they cannot be changed, and the same value may appear more than once.

Each item also has an index. Counting starts at zero, so index [0] is the first item, index [1] is the next one, and so on.


Ordered

Being ordered means a tuple keeps its items in a set sequence, and that sequence stays put.


Unchangeable

A tuple is fixed once it exists: you can't change its items, add new ones or remove any.


Allow Duplicates

Because every item has its own index, two items can share a value without any conflict:

Example

Here "apple" and "cherry" each appear twice:

Prompt
Create a tuple that contains duplicate values and print it.
Output: complete Python program

Tuple Length

The len() function tells you how many items a tuple holds:

Example

Count the items and print the result:

Prompt
Create a tuple with three items and print the number of items using len().
Output: complete Python program

Create Tuple With One Item

A tuple with a single item needs a comma after that item. Without the comma, Python won't treat it as a tuple.

Example

The comma makes all the difference:

Prompt
Create a one-item tuple with a trailing comma and print its type. Then create one without the comma and print its type to show the difference.
Output: complete Python program

Tuple Items - Data Types

A tuple can hold values of any data type:

Example

One tuple each of strings, integers and booleans:

Prompt
Create three tuples: one with strings, one with integers, and one with boolean values.
Output: complete Python program

Example

A single tuple can also mix those types:

Prompt
Create a tuple containing a mix of strings, integers, and boolean values.
Output: complete Python program

type()

To Python, a tuple is an object whose data type is 'tuple':

Example

Ask Python for the type of a tuple:

Prompt
Create a tuple and print its data type using the type() function.
Output: complete Python program

The tuple() Constructor

You can also build a tuple by calling the tuple() constructor.

Example

Making a tuple with the tuple() constructor:

Prompt
Use the tuple() constructor to create a tuple and print it.
Output: complete Python program

Python Collections (Arrays)

Python gives you four collection data types, and each one behaves a little differently:

  • List keeps its order and can be changed. Duplicates are fine.
  • Tuple keeps its order but cannot be changed. Duplicates are fine.
  • Set has no order and no indexes, and is unchangeable*. Duplicates are not allowed.
  • Dictionary keeps its order** and can be changed. Duplicates are not allowed.

*You can't change the items of a set once they are in it, but you are free to add new ones or take existing ones out at any time.

**Dictionaries are ordered from Python 3.7 onward. In Python 3.6 and earlier they are unordered.

It pays to know these properties before you pick a type for your data. The right choice can keep the meaning of the data intact, and it can also make your program more efficient or more secure.