Tuples in Python — How to Store and Manipulate Immutable Collections of Data

Sari Lakkis
4 min readJun 15, 2023

In the previous articles, we explored lists, one of the most commonly used data structures in Python. In this article, we’ll be discussing another type of data structure in Python, tuples.

This article is part of the Learn Python From Scratch series.

Follow me for more on programming Sari Lakkis

What is a Tuple in Python?

Like lists, tuples are a collection of values but tuples are immutable, meaning they cannot be changed once they are created. This makes them useful for storing data that you don’t want to be accidentally modified.

Tuples can have different types of elements and are defined using parentheses instead of square brackets, like lists.

Creating Tuples in Python

To create a tuple, we use parentheses instead of square brackets. Here’s an example:

my_tuple = (1, 2, 'a', True)

We can also create a tuple with just one item by adding a trailing comma:

my_tuple = (1,)

Accessing Tuple Elements

We can access tuple elements using indexing, just like with lists. Here’s an example:

my_tuple = (1, 2, 'a', True)
print(my_tuple[0]) # Output: 1

We can also use negative indexing to access elements from the end of the tuple:

my_tuple = (1, 2, 'a', True)
print(my_tuple[-1]) # Output: True

Slicing Tuples

We can also slice tuples, just like with lists, for example:

my_tuple = (1, 2, 'a', True)
print(my_tuple[1:3]) # Output: (2, 'a')

Concatenating Tuples

Tuples in Python can be concatenated using the + operator. This allows us to combine two or more tuples into a single tuple, for example:

tuple1 = (1, 2, 3)
tuple2 = ('a', 'b', 'c')
concatenated_tuple = tuple1 + tuple2
print(concatenated_tuple) # Output: (1, 2, 3, 'a', 'b', 'c')

In the example above, we have two tuples, tuple1 and tuple2. By using the + operator, we can concatenate these two tuples to create a new tuple concatenated_tuple, which contains all the elements from both tuple1 and tuple2.

We can also use the * operator to create a new tuple by repeating an existing tuple multiple times. For example:

tuple1 = (1, 2, 3)
repeated_tuple = tuple1 * 3
print(repeated_tuple) # Output: (1, 2, 3, 1, 2, 3, 1, 2, 3)

Concatenating and repeating tuples can be useful when we want to combine or replicate existing data in our programs.

Tuples are Immutable in Python

Unlike with lists, we cannot modify a tuple once it has been created. If we try to modify a tuple, Python will raise a TypeError:

my_tuple = (1, 2, 'a', True)
my_tuple[0] = 6 # Raises TypeError: 'tuple' object does not support item assignment

Tuples are useful when we want to ensure that the data we are working with is not accidentally modified. For example, if we are working with a set of coordinates, we may want to use a tuple to represent them:

coordinates = (1, 2)

In this case, we know that the coordinates cannot be modified once they are created, ensuring that our data remains accurate.

What if We Need To Modify a Tuple?

Since tuples are immutable, the recommended approach to modify a tuple is to create a new tuple with the desired changes. We can achieve this using tuple concatenation and slicing.

Let’s take an example starting with the original tuple my_tuple containing the values (1, 2, 3). By using tuple slicing and concatenation, we can create a new tuple modified_tuple with the desired modifications (1, 4, 3). We can do the following:

my_tuple = (1, 2, 3)
modified_tuple = my_tuple[:1] + (4,) + my_tuple[2:]
print(modified_tuple) # Output: (1, 4, 3)
  • my_tuple[:1] extracts the first element of my_tuple, which is (1).
  • (4,) is a new tuple containing the modified value (4) that we want to replace the second element with.
  • my_tuple[2:] extracts the third element of my_tuple, which is (3).

By concatenating these slices using the + operator, we create the modified tuple (1, 4, 3).

Iterating over Tuples

We can iterate over tuples using the in operator for example the following expression loops over all the elements in the tuple and add them.

my_tuple = (1,2,3,4)
sum = 0
for i in my_tuple:
sum += i

print("sum is",sum) # Output: sum is 10

Using Tuples to Return Multiple Values From a Function

Our problem in this article is to calculate the area and perimeter of a rectangle. We want to do that using one function.

Using tuples in Python allows us to return multiple values from a function.

So, let’s define a function called calculate_rectangle_properties that takes the length and width of a rectangle as parameters.

The function calculates the area and perimeter of the rectangle and returns a tuple (area, perimeter):

def calculate_rectangle_properties(length, width):
area = length * width
perimeter = 2 * (length + width)
return (area, perimeter)

We can call this function that returns both area and perimeter at the same time.

rectangle = calculate_rectangle_properties(5, 3)
print(rectangle) # Output: (15, 16)

When we call the function with the values 5 and 3, the result is a tuple we'll call it a rectangle variable. If we print the rectangle tuple, it gives us the output (15, 16), representing the area and perimeter.

Conclusion

In this article, we explored tuples in Python. Tuples are immutable, meaning they cannot be changed once they are created. Tuples can have different types of elements and are defined using parentheses instead of square brackets. We can access elements using indexing and slicing, just like with lists. Tuples are useful when we want to ensure that the data we are working with is not accidentally modified.

This article is part of the Learn Python From Scratch series.

--

--

Sari Lakkis

Professor | Consultant | Researcher. I give advice on strategy, design, and implementation of software that solves business problems to companies and startups.