Python Tuple

Introduction

A Python tuple is a collection of ordered items, much like a list. However, tuples differ from lists in a fundamental way: they are immutable, meaning their elements cannot be changed after creation. This immutability makes tuples simpler and more efficient data structures. They are useful for situations where you need to store a fixed set of values that should not be modified during program execution. Tuples have various use cases such as representing coordinates, defining constant values, and as keys in dictionaries.

Tuple Literals

A tuple is defined using parentheses () with comma-separated items inside.

The following is an example of a tuple that contains integers and strings.

Example:

my_tuple = (1, "two", 3, "four", 5)

You can create an empty tuple by using empty parentheses.

Example:

empty_tuple = ()

A tuple with just one item should include a trailing comma to distinguish it from a regular parentheses pair, which groups an expression.

Example:

single_item_tuple = (1,)

Comma-Separated Lists Create Tuples

You can also create a tuple without using parentheses by simply writing a comma-separated list of values. Python interprets this as a tuple in contexts where there is no other meaning.

my_tuple = 1, 2, 3
print(my_tuple)
print(type(my_tuple))

Output:

(1, 2, 3)
<class 'tuple'>

In this example, my_tuple is a tuple containing three integers. The absence of parentheses doesn’t change the tuple’s behavior. However, using parentheses for clarity is often recommended.

The Python Tuple Constructor

Tuples can be created using the tuple() constructor by passing an iterable object. This constructor will convert the iterable into a tuple.

Example with a list:

my_list = [1, 2, 3, 4]
print(tuple(my_list))

Output:

(1, 2, 3, 4)

Example with a string:

print(tuple("abcde"))

Output:

('a', 'b', 'c', 'd', 'e')

Finding the Length of a Tuple

To determine the number of elements in a tuple, you can use the built-in len() function, just as you would with lists.

Example:

my_list = (1, "two", 3, "four", 5)
print(len(my_list))

Output:

5

Accessing Items in a Python Tuple (Indexing)

Tuples can be indexed in the same way lists can, with square brackets []. The first item has index 0, and negative indexes mean counting from the end of the tuple, with the last item having an index of -1, the second-to-last item -2, and so on.

Example:

my_tuple = ('a', 'b', 'c', 'd')
print(my_tuple[0])
print(my_tuple[2])
print(my_tuple[-1])

Output:

a
c
d

Tuple Immutability

As mentioned in the introduction, tuple are immutable. Therefore, unlike lists, they have no operations available to change their internal state.

For instance, attempting to assign a value at a specific index of a tuple will result in an error.

t = (1, 2, 3)
t[1] = 20

Output:

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

Tuple Concatenation

You can concatenate two or more tuples using the + operator to create a new tuple without changing the original ones.

tuple1 = (1, 2, 3)
tuple2 = ("a", "b", "c")
combined_tuple = tuple1 + tuple2  # --> (1, 2, 3, 'a', 'b', 'c')

Output:

(1, 2, 3)
('a', 'b', 'c')
(1, 2, 3, 'a', 'b', 'c')

In this example, combined_tuple is (1, 2, 3, "a", "b", "c"), and the original tuple1 and tuple2 remain unchanged.

Summary & Reference for Python Tuple Basics

A Python tuple is a collection of ordered items similar to a list but except that it’s immutable.


Tuple literals are defined using parentheses () with comma-separated items inside.

my_tuple = (1, "two", 3, "four", 5)

() is the empty tuple.


A Single-item tuple requires a trailing comma to distinguish it from a regular parentheses pair.

my_tuple = (1,)

Tuples can also be created without parentheses by simply writing a comma-separated list of values. In contexts where there is no other syntactical meaning, Python interprets this as a tuple.

my_tuple = 1, 2, 3

Tuples can be constructed using the tuple() constructor by passing an iterable object, making it versatile for converting different iterable types into tuples.

my_list = [1, 2, 3, 4]
tuple_from_list = tuple(my_list)  # --> (1, 2, 3, 4)

To determine the number of elements in a tuple, you can use the len() function, just like with lists.

len(my_tuple)  # --> 5

Tuple items are accessed the same way as lists, using square brackets [] and an integer index, with the first item having index 0, second item index 1, etc.

my_tuple[2]  # --> 3

Negative indexes can be used to access items from the end of the tuple, with the last item having index -1, next to last -2, etc.

my_tuple[-1]  # --> 5

Unlike lists, tuples are immutable, meaning they cannot be modified once created. Attempting to change a tuple’s elements will result in an error.

my_tuple[2] = 30  # TypeError: 'tuple' object does not support item assignment

Concatenate two or more tuples using the + operator to create a new tuple.

tuple1 = (1, 2, 3)
tuple2 = ("a", "b", "c")
combined_tuple = tuple1 + tuple2  # --> (1, 2, 3, 'a', 'b', 'c')