In this tutorial you’ll learn what tuples are. In computer science, a tuple is an ordered and finite sequence of elements. Tuples are similar to lists, but there are some key differences that we will discuss throughout this tutorial.
Tuple Syntax
The first main difference between tuples and lists is related to the syntax. A tuple is written with parentheses, while a list is written with square brackets.
my_tuple = (1, 2, 3, 4, 5) # define a tuple containing integers
string_tuple = ("this", "is", "a", "tuple") # define a tuple containing strings
mixed_tuple = (1, "Hello, world!", True, 4.5) # define a tuple containing different data types
As you can see, a tuple can contain several data types (look at line 5) similarly to lists. However, we define a tuple using the parentheses.
Elements in a tuple can be accessed via indexing, with the same syntax used for lists. In fact, elements in a tuple have a well defined order.
numbers = (5, 6, 7, 8, 9, 10, 11, 12) # define a tuple first_item = numbers[0] # first element last_item = numbers[-1] # last element last_item_2 = numbers[len(numbers) - 1] # alternative to access the last element items = numbers[3:6] # from index 3 to index 5 (both inclusive) first_two_items = numbers[:2] # first item and second item
The examples above show that the process of indexing tuples is the same as for lists. Therefore, we continue using the square brackets to access the elements through their index. Particularly, line 6 demonstrates an alternative way to access the last element of a tuple (or list): we know that the last element in an ordered sequence (tuple or list) has an index equal to the length of the sequence minus one. Try manually counting the index of the last element in numbers tuple at line 1. It’s 7, which is equal to the length of the tuple (8) minus 1.
However, another difference between tuples and lists can be noted in this example. Whereas slicing (line 8 and line 10) returns a list when applied to a list, it returns a tuple when applied to a tuple. Clearly, both items and first_two_items are tuples in terms of data types.
Tuple Methods
Tuples are immutable objects. It means that once a tuple is created, its elements cannot be modified. Consequently, tuples cannot support addition, removal or updating of elements. Due to their immutability, tuples have only two methods. Instead, lists, which are mutable objects (look at the previous lesson, List Methods paragraph), have many more methods.
numbers = (3, 3, 5, 3, 9, 10, 1) count_of_three = numbers.count(3) # how many occurrences of 3 find_index = numbers.count(10) # the index of the first occurrence of 10
More precisely, the count() method returns the number of occurrences of a specified value in a tuple. The index() method returns the index of the first occurrence of a specified value in a tuple. Both count() and index() are applicable to lists, although we haven’t talked about them directly in the previous lesson on lists.
Tuple Functions
Tuple functions are built-in Python functions that can be used with tuples as arguments. We’ve already defined lists functions. As you will note, the same functions are applicable to tuples. Let’s give some examples:
my_tuple = (0, 1, 2, 3, 4, 5) length = len(my_tuple) # returns the length of the tuple: Output 6 total = sum(my_tuple) # sum of all elements in the tuple: Output 15 min_value = min(my_tuple) # the smallest value in the tuple max_value = max(my_tuple) # the largest value in the tuple
Here are only some examples of tuples functions. We have already used these functions for lists. Feel free to find other functions and apply them to lists or tuples. Some suggestions: any(), all(), zip(), enumerate().
Tuple Iteration
A tuple is an iterable object, similarly to a list. It means that both tuples and lists can be looped over using a for loop.
my_tuple = (8, 10, 12, 14, 16, 18, 20)
for element in my_tuple:
print(element)
for index in range(len(my_tuple)): # alternative
print(my_tuple[index])
Mutable vs Immutable
As we discussed earlier, a key difference between tuples and lists is related to the concept of immutability. When working with lists, it is possible to add, remove or updatie their elements using the well-defined methods. However, this is not possible when working with tuples.
my_tuple = (1, 2, 3) my_tuple[0] = 10 # try a replacement # TypeError: 'tuple' object does not support item assignment
This code generates a TypeError, due to the immutability of the tuples.
Let’s give another example:
my_tuple = (1, 2, 3, 4, 5) my_tuple.append(6) # AttributeError: 'tuple' object has no attribute 'append'
The code above generates an AttributeError.
These are the reasons why tuples are very useful when you want to create a collection of items that should not be changed.