Tuples in Python (23/100 Days of Python)

Martin Mirakyan
4 min readJan 24, 2023

--

Day 23 of the “100 Days of Python” blog post series covering tuples

A tuple is a data structure in Python that allows you to store a collection of items. Tuples are similar to lists, but unlike lists, tuples are immutable, meaning their elements cannot be changed once they are created. This makes them especially useful for situations where you want to store a fixed set of data that should not be modified.

Creating Tuples

To create a tuple in Python, one needs to put the data inside parentheses and separate the items with commas:

pi = (1, 'pi', 3.14)   # Create a tuple named pi
e = (2, 'e', 2.718) # Create a tuple named e
single_item = (1,) # Create a tuple with only a single element

Tuples can also be created without parentheses:

pi = 1, 'pi', 3.14   # Create a tuple named pi
e = 2, 'e', 2.718 # Create a tuple named e
single_item = 1, # Create a tuple with only 1 - be careful with commas

It is important to note that creating single-element tuples without parentheses and with a trailing comma can be a source of bugs. This is because the trailing comma can be easily missed, leading to the creation of a single-element tuple instead of the intended variable. To avoid such bugs, it is best practice to use parentheses when creating tuples, especially when creating single-element tuples:

one = (1)      # An integer 1
one = (1,) # Creates a tuple (1,)
one = 1, # Creates a tuple (1,)
one = 1 # An integer 1

Accessing Elements

Just like lists, tuples can be accessed using indexing. You can use both positive and negative indexing to access elements of a tuple:

user = ('4323asdr132', 'Anna', 'anna@gmail.com')
print(f'User with ID {user[0]} has email: {user[-1]}')
print('The name of the user is', user[1])

This makes working with tuples very convenient as the process is very similar to working with lists. Yet, it’s important to always remember that tuples are immutable, which means that once they are created, the elements of a tuple cannot be modified. If you try to modify an element of a tuple, you will receive a TypeError:

my_tuple = (1, 2, 3)
my_tuple[1] = 4

This program will result in a TypeError: 'tuple' object does not support item assignment.

It’s not possible to modify the elements of a tuple, however, there are 2 ways to obtain a new tuple by modifying the initial one:

  1. You can create a new tuple with the modified elements
  2. You can convert the tuple into a list, modify the element and then convert it back to a tuple
# 1. Create a new tuple with the modified elements
my_tuple = (1, 2, 3) # Create the initial tuple
my_tuple = my_tuple[:1] + (4,) + my_tuple[2:] # my_tuple[1] = 4
print(my_tuple) # (1, 4, 3)

# 2. tuple -> list + modify -> tuple
my_list = list(my_tuple) # tuple -> list
my_list[1] = 4 # Modify
my_tuple = tuple(my_list) # list -> tuple
print(my_tuple) # (1, 4, 3)

Unpacking Elements of a Tuple

One of the most powerful features of tuples is the ability to unpack them into separate variables. This can be extremely useful for working with multiple values at once. We can use this Python feature to conveniently work with multiple values:

user = ('4323asdr132', 'Anna', 'anna@gmail.com')
id, name, email = user
print(f'User with ID {id} has email: {email}')
print('The name of the user is', name)

location = location = (37.788022, -122.399797)
latitude, longitude = location
print(f'The user is currently at {latitude}, {longitude}')

Swapping Variables

You might have seen a code that swaps two variables in Python using commas and assignments:

a = 5
b = 10
a, b = b, a # a takes the value of b and b takes the value of a
print(a, b) # 10 5

This is actually possible thanks to tuples in Python. The right-hand side of the equation is a tuple, while the left-hand side performs tuple unpacking.

This popular feature is widely used but is not usually attributed to the powerful features of tuples in Python.

In summary, tuples are a valuable data structure in Python programming, providing developers with a way to store and organize collections of items. Their immutability makes them ideal for situations where data integrity and security are a priority.

What’s next?

--

--

Martin Mirakyan
Martin Mirakyan

Written by Martin Mirakyan

Software Engineer | Machine Learning | Founder of Profound Academy (https://profound.academy)

No responses yet