This notebook contains an excerpt from the Python Programming and Numerical Methods - A Guide for Engineers and Scientists, the content is also available at Berkeley Python Numerical Methods.
The copyright of the book belongs to Elsevier. We also have this interactive book online for a better learning experience. The code is released under the MIT license. If you find this content useful, please consider supporting the work on Elsevier or Amazon!
< 2.3 Data Structure - Lists | Contents | 2.5 Data Structure - Sets >
Data Structure - Tuples¶
Let’s learn one more different sequence data structure in Python - Tuples. It is usually defined by using a pair of parentheses ( ) , and its elements are separated by commas. For example:
tuple_1 = (1, 2, 3, 2)
tuple_1
(1, 2, 3, 2)
As strings and lists, they way for indexing the tuples, slicing the elements, and even some methods are very similar.
TRY IT! Get the length of tuple_1.
len(tuple_1)
4
TRY IT! Get the elements from index 1 to 3 for tuple_1.
tuple_1[1:4]
(2, 3, 2)
TRY IT! Count the occurrence for number 2 in tuple_1.
tuple_1.count(2)
2
You may ask, what’s the difference between lists and tuples? If they are similar to each other, why do we need another sequence data structure?
Well, tuples are created for a reason. From the Python documentation:
Though tuples may seem similar to lists, they are often used in different situations and for different purposes. Tuples are immutable, and usually contain a heterogeneous sequence of elements that are accessed via unpacking (see later in this section) or indexing (or even by attribute in the case of named tuples). Lists are mutable, and their elements are usually homogeneous and are accessed by iterating over the list.
What does it mean by immutable? It means the elements in the tuple, once defined, they can not be changed. But elements in a list can be changed without any problem. For example:
list_1 = [1, 2, 3]
list_1[2] = 1
list_1
[1, 2, 1]
tuple_1[2] = 1
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-6-76fb6b169c14> in <module>()
----> 1 tuple_1[2] = 1
TypeError: 'tuple' object does not support item assignment
What does heterogeneous mean? Tuples usually contain a heterogeneous sequence of elements, while lists usually contain a homogeneous sequence. Let’s see an example, that we have a list that contains different fruits. Usually the name of the fruits could be stored in a list, since they are homogeneous. Now we want to have a data structure to store how many fruit do we have for each type, this is usually where the tuples comes in, since the name of the fruit and the number are heterogeneous. Such as (‘apple’, 3) which means we have 3 apples.
# a fruit list
['apple', 'banana', 'orange', 'pear']
['apple', 'banana', 'orange', 'pear']
# a list of (fruit, number) pairs
[('apple', 3), ('banana', 4) , ('orange', 1), ('pear', 4)]
[('apple', 3), ('banana', 4), ('orange', 1), ('pear', 4)]
Tuples could be accessed by unpacking, it requires that the number of variables on the left side of the equals sign equals to the number of elements in the sequence.
a, b, c = list_1
print(a, b, c)
1 2 1
NOTE! The opposite operation to unpacking is packing as shown in the following example. We could see that we don’t need the parentheses to define a tuple, but it is always good to have that.
list_2 = 2, 4, 5
list_2
(2, 4, 5)
< 2.3 Data Structure - Lists | Contents | 2.5 Data Structure - Sets >