-
Book Overview & Buying
-
Table Of Contents
The Python Workshop Second Edition - Second Edition
By :
A tuple object is similar to a list, but it cannot be changed. Tuples are immutable sequences, which means their values cannot be changed after initialization. You can use a tuple to represent fixed collections of items:
Figure 2.17 – A representation of a Python tuple with a positive index
For instance, you can define the weekdays using a list, as follows:
weekdays_list = ['Monday', 'Tuesday', 'Wednesday',
'Thursday','Friday','Saturday', 'Sunday']
However, this does not guarantee that the values will remain unchanged throughout their life because a list is mutable. What you can do is define it using a tuple, as shown in the following code:
weekdays_tuple = ('Monday', 'Tuesday', 'Wednesday',
'Thursday','Friday','Saturday', 'Sunday')
As tuples are immutable, you can be certain that the values are consistent throughout the entire program and will not be modified accidentally or unintentionally. In the next exercise, you will explore the different properties tuples provide a Python developer.
In this exercise, you will learn about the different properties of a tuple:
t:t = ('ballet', 'modern', 'hip-hop')print(len(t))The output is as follows:
3
Note
Remember, a tuple is immutable; therefore, you can’t use the append method to add a new item to an existing tuple. You can’t change the value of any existing tuple’s elements since both of the following statements will raise an error.
t[2] = 'jazz'The output is as follows:
Figure 2.18 – Errors occur when we try to modify the values of a tuple object
The only way to get around this is to create a new tuple by concatenating the existing tuple with other new items.
jazz and tap, to our tuple, t. This will give us a new tuple. Note that the existing t tuple remains unchanged:print(t + ('jazz', 'tap'))print(t)The output is as follows:
('ballet', 'modern', 'hip-hop', 'jazz', 'tap')
('ballet', 'modern', 'hip-hop')
t_mixed = 'jazz', True, 3print(t_mixed)t_dance = ('jazz',3), ('ballroom',2), ('contemporary',5)print(t_dance)Tuples also support mixed types and nesting, just like lists and dictionaries. You can also declare a tuple without using parentheses, as shown in the code you entered in this step.
The output is as follows:
('jazz', True, 3)
(('jazz', 3), ('ballroom', 2), ('contemporary', 5))
Sometimes, you obtain information from multiple lists. For instance, you might have a list to store the names of products and another list just to store the quantity of those products. You can aggregate these lists using the zip() method.
The zip() method maps a similar index of multiple containers so that they can be used as a single object. You will try this out in the following exercise.
In this exercise, you will work on the concept of dictionaries by combining different types of data structures. You will use the zip() method to manipulate the dictionary with our shopping list. The following steps will help you understand the zip() method:
items = ['apple', 'orange', 'banana']quantity = [5,3,2]Here, you have created a list of items and a list of quantity. Also, you have assigned values to these lists.
zip() function to combine the two lists into a list of tuples:orders = zip(items,quantity)print(orders)This gives us a zip() object with the following output:
<zip object at 0x0000000005BF1088>
zip() object into a list:orders = zip(items,quantity)print(list(orders))The output is as follows:
[('apple', 5), ('orange', 3), ('banana', 2)]
zip() object into a tuple:orders = zip(items,quantity)print(tuple(orders))Let’s see the output:
(('apple', 5), ('orange', 3), ('banana', 2))
zip() object into a dictionary:orders = zip(items,quantity)print(dict(orders))Let’s see the output:
{'apple': 5, 'orange': 3, 'banana': 2}
Did you realize that you have to call orders = zip(items,quantity) every time? In this exercise, you will have noticed that a zip() object is an iterator, so once it has been converted into a list, tuple, or dictionary, it is considered a full iteration and it will not be able to generate any more values.