-
Book Overview & Buying
-
Table Of Contents
Hands-On Data Structures and Algorithms with Python – Third Edition - Third Edition
By :
We have discussed basic data types. Next, we discuss complex data types, which are mapping data types, in other words, dictionary, and set data types, namely, set and frozenset. We will discuss these data types in detail in this section.
In Python, a dictionary is another of the important data types, similar to a list, in the sense that it is also a collection of objects. It stores the data in unordered {key-value} pairs; a key must be of a hashable and immutable data type, and value can be any arbitrary Python object. In this context, an object is hashable if it has a hash value that does not change during its lifetime in the program.
Items in the dictionary are enclosed in curly braces, {}, separated by a comma, and can be created using the {key:value} syntax, as shown below:
dict = {
<key>: <value>,
<key>: <value>,
.
.
.
<key>: <value>
}
Keys in dictionaries are case-sensitive, they should be unique, and cannot be duplicated; however, the values in the dictionary can be duplicated. For example, the following code can be used to create a dictionary:
my_dict = {'1': 'data',
'2': 'structure',
'3': 'python',
'4': 'programming',
'5': 'language'
}
Figure 1.5 shows the {key-value} pairs created by the preceding piece of code:

Figure 1.5: Example dictionary data structure
Values in a dictionary can be fetched based on the key. For example: my_dict['1'] gives data as the output.
The dictionary data type is mutable and dynamic. It differs from lists in the sense that dictionary elements can be accessed using keys, whereas the list elements are accessed via indexing. Table 1.4 shows different characteristics of the dictionary data structure with examples:
|
Item |
Example |
|
Creating a dictionary, and accessing elements from a dictionary |
|
|
|
|
|
Length of the dictionary |
|
Table 1.4: Characteristics of dictionary data structures with examples
Python also includes the dictionary methods as shown in Table 1.5:
|
Function |
Description |
Example |
|
|
Removes all elements from a dictionary. |
|
|
|
Searches the dictionary for a key and returns the corresponding value, if it is found; otherwise, it returns |
|
|
|
Returns a list of dictionary items in (key, value) pairs. |
|
|
|
Returns a list of dictionary keys. |
|
|
|
Returns a list of dictionary values. |
|
|
|
If a given key is present in the dictionary, then this function will remove the key and return the associated value. |
|
|
|
This method removes the last key-value pair added in the dictionary and returns it as a tuple. |
|
|
|
Merges one dictionary with another. Firstly, it checks whether a key of the second dictionary is present in the first dictionary; the corresponding value is then updated. If the key is not present in the first dictionary, then the key-value pair is added. |
|
Table 1.5: List of methods of dictionary data structures
A set is an unordered collection of hashable objects. It is iterable, mutable, and has unique elements. The order of the elements is also not defined. While the addition and removal of items are allowed, the items themselves within the set must be immutable and hashable. Sets support membership testing operators (in, not in), and operations such as intersection, union, difference, and symmetric difference. Sets cannot contain duplicate items. They are created by using the built-in set() function or curly braces {}. A set() returns a set object from an iterable. For example:
x1 = set(['and', 'python', 'data', 'structure'])
print(x1)
print(type(x1))
x2 = {'and', 'python', 'data', 'structure'}
print(x2)
The output will be as follows:
{'python', 'structure', 'data', 'and'}
<class 'set'>
{'python', 'structure', 'data', 'and'}
It is important to note that sets are unordered data structures, and the order of items in sets is not preserved. Therefore, your outputs in this section may be slightly different than those displayed here. However, this does not affect the function of the operations we will be demonstrating in this section.
Sets are generally used to perform mathematical operations, such as intersection, union, difference, and complement. The len() method gives the number of items in a set, and the in and not in operators can be used in sets to test for membership:
x = {'data', 'structure', 'and', 'python'}
print(len(x))
print('structure' in x)
The output will be as follows:
4
True
The most commonly used methods and operations that can be applied to set data structures are as follows. The union of the two sets, say, x1 and x2, is a set that consists of all elements in either set:
x1 = {'data', 'structure'}
x2 = {'python', 'java', 'c', 'data'}
Figure 1.6 shows a Venn diagram demonstrating the relationship between the two sets:

Figure 1.6: Venn diagram of sets
A description of the various operations that can be applied on set type variables is shown, with examples, in Table 1.6:
|
Description |
Example sample code |
|
Union of two sets, |
|
|
Intersection of sets: to compute the intersection of two sets, an |
|
|
The difference between sets can be obtained using |
|
|
Symmetric difference can be obtained using |
|
|
To test whether a set is a subset of another, use |
|
Table 1.6: Description of various operations applicable to set type variables
In Python, frozenset is another built-in type data structure, which is, in all respects, exactly like a set, except that it is immutable, and so cannot be changed after creation. The order of the elements is also undefined. A frozenset is created by using the built-in function frozenset():
x = frozenset(['data', 'structure', 'and', 'python'])
print(x)
The output is:
frozenset({'python', 'structure', 'data', 'and'})
Frozensets are useful when we want to use a set but require the use of an immutable object. Moreover, it is not possible to use set elements in the set, since they must also be immutable. Consider an example:
a11 = set(['data'])
a21 = set(['structure'])
a31 = set(['python'])
x1 = {a11, a21, a31}
The output will be:
TypeError: unhashable type: 'set'
Now with frozenset:
a1 = frozenset(['data'])
a2 = frozenset(['structure'])
a3 = frozenset(['python'])
x = {a1, a2, a3}
print(x)
The output is:
{frozenset({'structure'}), frozenset({'python'}), frozenset({'data'})}
In the above example, we create a set x of frozensets (a1, a2, and a3), which is possible because the frozensets are immutable.
We have discussed the most important and popular data types available in Python. Python also provides a collection of other important methods and modules, which we will discuss in the next section.
Change the font size
Change margin width
Change background colour