Skip to content
Home / Fundamentals

Python Tuple

A tuple is a sequence data type in Python that is similar to a list, but it is immutable, meaning that once you create a tuple, you cannot change its elements.

Tuple Syntax

Tuples are defined using parentheses () instead of square brackets [], like this:

# define a tuple
t = (1, 2, 3)

# access elements in a tuple
print(t[0])  # 1

# tuples can also contain different data types
t = (1, 'a', 3.14)

You can also create a tuple by enclosing a sequence of elements in parentheses:

# create a tuple from a list
lst = [1, 2, 3]
t = tuple(lst)
print(t)  # (1, 2, 3)

# create a tuple from a string
s = 'abc'
t = tuple(s)
print(t)  # ('a', 'b', 'c')

Tuple Characteristics

Tuples are immutable, meaning that you cannot change the elements of a tuple after it is created. For example, you cannot assign a new value to an element of a tuple:

t = (1, 2, 3)
t[0] = 4  # This will raise a TypeError

Tuples can be indexed and sliced like lists. For example:

t = (1, 2, 3, 4, 5)
print(t[0])  # 1
print(t[-1]) # 5
print(t[1:3])  # (2, 3)

Tuples can be nested, meaning that you can have a tuple inside of another tuple:

t = (1, 2, (3, 4))
print(t[2])  # (3, 4)

Tuple Use Cases

Here are some common use cases for tuples:

You can use tuples to store data that you don't want to change. For example, if you want to store the names of the days of the week, you might use a tuple like this:

days_of_the_week = ('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday')

Tuples can be used as keys in dictionaries. Since tuples are immutable, they can be used to represent fixed data that doesn't change, such as a record of a person's name and birth date. For example:

person = {
    ('John', 'Doe'): '[email protected]',
    ('Jane', 'Doe'): '[email protected]'
}

Tuples can be used to return multiple values from a function. For example:

def divide(x, y):
    quotient = x // y
    remainder = x % y
    return quotient, remainder

result = divide(10, 3)
print(result)  # (3, 1)

Tuple Best Practices

Use tuples when you want to store data that shouldn't be changed. Since tuples are immutable, they can be used to store data that you don't want to be modified, such as constants or configuration data.

Use tuples to return multiple values from a function. As mentioned in the previous example, tuples can be used to return multiple values from a function, which can be convenient and more efficient than returning a list or dictionary.

Use tuples as keys in dictionaries when you have data that shouldn't be changed. Since tuples are immutable, they can be used as keys in dictionaries to represent fixed data that doesn't change, such as a record of a person's name and birth date.

Use tuples to preserve the original order of data. Since tuples are ordered sequences, they can be used to preserve the original order of data, such as a list of names or a list of records.

Use tuples for small, fixed-length data structures. Since tuples are more efficient than lists in terms of memory usage and performance, they can be a good choice for small, fixed-length data structures where you don't need to modify the elements.