Python Tuples
Tuples in Python are immutable sequences of arbitrary elements.
- defined by enclosing elements in parentheses
(), separated by commas - e.g.,
my_tuple = (1, "a", 3.14) - often used in situations where an immutable sequence of data is needed
- can be used as keys in dictionaries, which require immutability for hash mapping
- have a smaller memory footprint compared to lists
- more efficient for large datasets
# Define a tuple for a network device
network_device = ("192.168.1.1", "cisco", "admin", "password")
# Unpack the tuple
ip_address, device_type, username, password = network_device
# Use the unpacked values
print(f"Connecting to device {device_type} at {ip_address} using username {username}")Tuple Length
Can determine the number of items contained in a tuple using the built-in len() function.
- returns the number of elements in an iterable, including tuples
Tuples with Single Item
- Create by placing a single value inside parentheses
()followed with a comma,- e.g.,
single_item_tuple = ("admin",)
- e.g.,
- trailing comma is necessary to distinguish the tuple from a simple parenthesized expression
- useful for:
- when a function is expected to return a tuple, but there is only one value to return, a single-item tuple can be used
- ensures that the function’s return type is consistent
- when a function is expected to return a tuple, but there is only one value to return, a single-item tuple can be used
The Tuple Constructor
The tuple() constructor in Python is a built-in function that allows the creation of a tuple.
- can take an iterable (like a list or a string) as an argument and convert it into a tuple
- useful when there is a need to ensure the immutability of a sequence of data
Modifying Tuples
Accessing Item in a Tuple
- access items using indexing
- e.g.,
tuple[2]
- e.g.,
- ranges:
tuple[start:stop] - negative indexes:
tuple[-3:] - Check if an item exists in a tuple with
in'item' in tuple- returns
TrueorFalse
Updating Tuples
- Tuples in Python are immutable
- values cannot be changed once they are created
- but there is a workaround:
- convert the tuple into a list, changing the list, and then convert the list back into a tuple
# Define a tuple for a network device
network_device = ("192.168.1.1", "cisco", "admin", "password")
# Convert the tuple into a list
network_device_list = list(network_device)
# Change the IP address in the list
network_device_list[0] = "192.168.2.2"
# Convert the list back into a tuple
network_device = tuple(network_device_list)
# Print the new tuple
print(f"Network device: {network_device}")Add Items to Tuple
- items cannot be added directly
- workaround:
- concatenate the tuple with another tuple containing the new items
# Define a tuple for a network device
network_device = ("192.168.1.1", "cisco", "admin", "password")
# Define a new tuple with additional information
additional_info = ("port 22", "ssh")
# Concatenate the tuples
network_device = network_device + additional_info
# Print the new tuple
print(f"Network device: {network_device}")Remove Item From Tuple
- Cannot remove item directly
- workaround:
- convert the tuple to a list, removing the item, and then convert the list back to a tuple
# Original tuple with IP address, hostname, and model
device_info = ("192.168.1.1", "switch1", "Cisco 3750")
# Convert tuple to list
device_list = list(device_info)
# Remove old model from list
device_list.remove("Cisco 3750")
# Add new model to list
device_list.append("Cisco 3850")
# Convert list back to tuple
device_info = tuple(device_list)