10.1 Tuple and its Properties
Tuples are one of the four built-in collection types in Python.
As you probably know, Python has several built-in data types for storing groups of objects. These include list
— list, tuple
— tuple, set
— set, and dictionary
— dictionary. Today we're checking out the second and one of the simplest — the tuple.
A (tuple)
lets you store multiple items. Each item has its own index, making the tuple ordered — ordered. This makes it similar to a list.
You can't change a tuple — once it's created, it's locked in, making it perfect for storing data that shouldn't change in your program. So its second characteristic is immutability (immutable).
As for duplicates — a tuple can hold the same item more than once — allow duplicates. Again, very similar to a list.
Pro Tip: Think of a tuple as an immutable list that uses round brackets instead of square ones ()
instead of []
.
10.2 Creating a Tuple
Tuples are immutable sequences of elements and you can create them in different ways. Here are the most common:
Direct declaration:
You can create tuples directly using parentheses () and commas to separate items.
tuple1 = (1, 2, 3)
tuple2 = ("apple", "banana", "cherry", "apple", "cherry")
tuple3 = (1, "apple", True)
Without parentheses:
Python lets you create tuples without using parentheses, just separating items with commas.
tuple1 = 1, 2, 3
tuple2 = "apple", "banana", "cherry", "apple", "cherry"
tuple3 = 1, "apple", True
One-Item Tuple:
To create a tuple with one item, you need to put a comma after that item
.
tuple1 = (1,)
tuple2 = ("apple",)
tuple3 = (True,)
Using tuple()
:
The tuple()
function converts an iterable into a tuple. This can be a list, string, or any other iterable.
list_to_tuple = tuple([1, 2, 3])
10.3 Unpacking Tuple Items
When working with tuples, you can pack multiple items into one tuple or unpack a tuple into multiple variables, simplifying the code:
Basic Unpacking
An example of unpacking a tuple into variables:
my_tuple = (1, 2, 3)
x, y, z = my_tuple
Here the variables x
, y
, and z
get the values 1
, 2
, and 3
respectively.
Extended Unpacking
Python also supports extended unpacking, letting you extract parts of a tuple into a list using the * symbol:
a, *b, c = (1, 2, 3, 4, 5) # a = 1, b = [2, 3, 4], c = 5
In this example, a
and c
get the first and last values, while b
becomes a list with all the rest.
Using in Functions
Tuple unpacking is often used when passing parameters to functions. It's especially handy when a function takes a variable number of arguments or when arguments are pre-packed into tuples or lists:
def func(a, b, c):
print(a, b, c)
values = (1, 2, 3)
func(*values)
Unpacking Peculiarities
Tuple unpacking needs an exact match between the number of tuple items and the number of variables they're assigned to, or an error will be generated. However, using *
to scoop up extra items can help dodge this error, making the code more flexible with data changes.
10.4 List of Methods
Tuples are immutable objects, so they have almost no methods. Just two 😊
The count(x)
Function:
Returns the number of times x
appears in the tuple. Useful for counting how many times specific elements show up.
t = (1, 2, 3, 2, 4, 2)
print(t.count(2)) # Prints 3
The index(x)
Function:
Returns the index of the first occurrence of element x
in the tuple. If the element isn't found, it raises a ValueError
exception.
t = (1, 2, 3, 2, 4, 2)
print(t.index(3)) # Prints 2
Even though these are all the tuple class methods, it doesn't mean you can't do anything with it. You can, it's just a bit trickier.
GO TO FULL VERSION