Tuples (optional)
Introduction¶
Tuples are an immutable sequence well-suited to contain items of differing data types. Unlike lists, tuples cannot be changed once created. This makes them useful for storing data that should not be modified.
Creating a Tuple¶
You can create a tuple by placing a comma-separated sequence of values inside parentheses:
switch = ("Cisco", "9300", 16)
model = switch[0:2]
print(model) # ('Cisco', '9300')
print(type(model)) # <class 'tuple'>
Unpacking a Tuple¶
Tuples can be unpacked into individual variables:
Immutability of Tuples¶
Attempting to change a value in a tuple will result in an error:
Practical Example¶
Consider a list of tuples representing network devices and their port counts:
devices = [
("192.168.1.12", 16),
("172.22.22.22", 32),
("10.9.8.7", 24),
]
for ip, ports in devices:
msg = f"""
IP Address: {ip}
Ports: {ports}
"""
print(msg)
Loops with Tuples¶
As you will find out, looping over lists of tuples and dictionaries are very closely related. And it is not much different from looping over lists.
When you read a loop like this, you can think of it as saying:
for each device in the tuple() of devices, print() the device.
You may have noticed the indented code block under the for loop. This block is called the loop body and contains the
code that is executed for each iteration of the loop.
What happens in this code example is that the for loop iterates over each item in the tuple. The device variable
is assigned the value of each item in the tuple() and then the loop executes the loop body. This process continues
until all items in the tuple have been processed. You don't need to know how many items are in the tuple; the loop will handle
that for you.
Lists of tuples¶
Now, let's take a look at a list of tuples. This is a list where each item is a tuple, rather than a string like in the previous
example. It works in the same way as looping over a list or tuple. Instead of device being assigned a string, it is assigned a tuple.
devices = [
("router2", "cisco_ios"),
("router17", "cisco_ios"),
("switch11", "cisco_ios"),
]
for device in devices:
print(f"{device = }")
device = ('router2', 'cisco_ios')
device = ('router17', 'cisco_ios')
device = ('switch11', 'cisco_ios')
However, using unpacking in the loop expression, we can assign each item in the tuple to a separate variable. As you may remember unpacking a tuple is done by assigning the tuple to a comma-separated list of variables.
Together, the unpacking and the for loop make it easy to work with lists of tuples:
device = 'router2'
os = 'cisco_ios'
device = 'router17'
os = 'cisco_ios'
device = 'switch11'
os = 'cisco_ios'
So easy to read and understand! And we can easily add more variables to the tuple and unpack them in the loop.
devices = [
("router2", "cisco_ios", "18.2.11"),
("router17", "cisco_ios", "18.2.14"),
("switch11", "cisco_ios", "18.2.14"),
]
for device, os, version in devices:
print(f"{device = }\n{os = }\n{version = }\n")
device = 'router2'
os = 'cisco_ios'
version = '18.2.11'
device = 'router17'
os = 'cisco_ios'
version = '18.2.14'
device = 'switch11'
os = 'cisco_ios'
version = '18.2.14'
Great stuff! We can now easily loop over lists of tuples and unpack the tuples to get the individual items. This makes the code
more readable and easier to understand. If you have a list of tuples, you should always consider using unpacking in the for loop.
Summary¶
In this section, we covered the basics of tuples in Python, including how to create, unpack, and understand their immutability. Tuples are a powerful tool for storing fixed collections of items, especially when the data should not be altered.
You also learned how to loop over lists of tuples and use unpacking to assign each item in the tuple to a separate variable. This technique enhances code readability and simplifies data handling when working with structured data.