Tuples are an ordered, immutable collection type in Python. That is, elements in Tuple are not modifiable, and neither are their positions.
Tuples are also indexed. This means we can access the elements of a Tuple using their positional index (starts with 0)
Tuples allow duplicates and different data types as their elements. Hence, it’s a flexible array-like alternative to storing data in Python with better performance than lists.
This post is about the basics of Tuples and the methods we can use on Tuples.
What is Tuples in Python?
Tuples are one of the four (Lists, Sets, Dictionary) data types to store a collection of items. Also, elements in Tuples are ordered and indexed. This array-like object type does not allow for modifications. Yet they allow duplicates.
These properties make Tuples more flexible than sets that don’t allow duplicates. But Tuples are a bit more rigid than lists that permit modification.
You can create a Tuple by specifying elements inside parenthesis.
You can have as many items as you want inside a Tuple. Yet, if there’s only one item, there has to be a trailing comma before you close the parenthesis.
You could also use a Tuple constructor to create them. Tuple constructors are helpful when you need to create Tuples programmatically or convert them from a list or set.
Tuples can have more than one type of element. It doesn’t have to be the same type.
How to access tuple elements?
Python tuples are ordered and indexed, which makes accessing elements easier. We can get a single item by its index or many of them in a range of indexes.
Indexing in Python starts with zero. The first element in the tuple has an index of 0. To access the 5th element in we must use index 4.
You can also specify a starting and ending index to select elements in a range of indexes. When you do a range selection on a Tuple, the return type is also a Tuple.
Sometimes you may want to select elements in steps. For instance, every other element in a Tuple. In that case, you can specify a range and the steps.
Tuples in Python support negative indexes. Thus to select the last element, you can use the index -1. For the third element from the last, you can use -3.
You could also use index range selection with negative indexes. For instance, you can use the negative index -1 to get every other element in a Tuple with no reference to the length of the Tuple.
Finding the index of an element in a Tuple
You may want to find the position (or the index) of an element inside a Tuple. You can use the index method on a tuple with the element as the argument to find its location.
Counting elements on a Tuple
Frequently we need to count the occurrence of a specific element in a tuple. In Python, you don’t have to loop through the elements and increment a variable to count them.
You can use the count method on a tuple with an element as its argument. The count method will return the number of times the element appeared in the list.
Modifying or Updating a Tuple
Tuples are immutable. That is, you can not edit the elements of a Tuple after you’ve created it.
You must use a list if you still want to edit tuple items.
If lists are incompatible with your code, you may convert them back to tuples after you change them.
For instance, you can append an item to a tuple as follows.
Deconstructing tuple elements.
Deconstructing or unpacking tuples is a clever way of accessing elements with less code. For instance, let’s say we have a tuple whose,
– the first element is the name of our hero;
– the second element is the age of our hero;
– the third element is if our hero is part of the Avengers and;
– finally, our hero’s weight.
Suppose we want to assign these values to individual variables; we can use tuple deconstructors.
As you can see, deconstructors are elegant and concise. These assignments would have taken four lines. But with deconstructors, we’ve assigned tuple elements to variables in a single line.
The ordered property of Tuples allows variable assignment with deconstructors.
If you don’t want all the elements of a Tuple unpacked, you can use an asterisk in the deconstructor assignment.
For instance, if we’re interested in a hero’s name and age, then we could do something like this. A variable with an asterisk will put all the remaining elements into this bucket. This would be a tuple too.
The asterisk in the tuple doesn’t have to be at the end. We can also take the first (or the first few) and the last (or the last few) and everything in the middle into three separate variables.
Loop through tuple elements
Tuples are a type of iterable in Python. This means you can loop through their elements with ‘for’ or ‘while’ loops. Since tuples are indexed, you can also loop through their index.
You can also loop through the elements of a tuple until a certain condition is met. You can use the while loop to do so.
You can find more examples of iterating over tuple elements.
Tuples in Python are immutable. Yet, two or more tuples can be joined together to form a new tuple.
Not a Medium member yet? Please use this link to become a member because I earn a commission for referring at no extra cost for you.